c# winform 連接服務器地址 實現數據交互-登錄功能


 

 

 

全局變量定義

//strhttp 是寫在app.config中的一個字符串,方便以后更改連接用

<appSettings>
<add key="strhttp" value="http://172.16.0.5:8080/ytw-web/"/>
</appSettings>

string strhttp = ConfigurationManager.AppSettings["strhttp"];
string Username = string.Empty;
string Password = string.Empty;

string struserID = string.Empty;

事件內的處理代碼

Username = textBox1.Text.Trim();
Password = textBox2.Text.Trim();

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(strhttp+"client/login.shtml?userName="+Username+"&passWord="+Password+"");
//申請超時時間
request.Timeout = 20000;
request.ServicePoint.ConnectionLimit = 100;

request.ReadWriteTimeout = 30000;

request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if(response.StatusCode != HttpStatusCode.OK)
return;
//得到的返回數據串
StreamReader sr = new StreamReader(response.GetResponseStream());
while (!sr.EndOfStream)
{
string s1 = sr.ReadLine();
//查找返回串的各個數據所在位置

//看具體返回串 調整下面代碼   主要是看自己的需求調整如下代碼
int Lenresult = s1.IndexOf("result");
string strreturn = s1.Substring(Lenresult + 9, 1);
if(Convert.ToInt32(strreturn)>0)
{
int LenuserID = s1.IndexOf("userID");
int lenlast = s1.LastIndexOf("}");
struserID = s1.Substring(LenuserID + 8, lenlast - (LenuserID + 8));
MainForm mf = new MainForm();

mf.Owner = this;
this.Hide();
mf.Show();
break;//運行完成以后要退出循環, 不然容易死循環
}
else
{
linkLabel1.Text = "登錄失敗";
break;//運行完成以后要退出循環, 不然容易死循環
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM