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