最近打算编个自动登录的小软件,怎奈自己是个小白,实在不知道哪错,希望大侠帮忙指点下!
1 private void Form1_Load(object sender, EventArgs e) 2 { 3 string str; 4 string postData; 5 postData = "user=106042108&pass=6283****21713&ip=172.20.15.210&type=2&action=%C1%AC%BD%D3%CD%F8%C2%E7"; 6 str=SendMsg(postData); 7 textBox1.Text = str; 8 } 9 public string SendMsg(string postData) 10 { 11 string url = "http://121.195.254.46/index.jsp"; 12 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 13 request.Method = "POST"; 14 request.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"; 15 request.Referer = "http://121.195.254.46/index.jsp"; 16 request.AllowAutoRedirect = true; 17 request.KeepAlive = true; 18 request.SendChunked = true; 19 request.ContentType = "text/html;charset=gb2312;"; 20 request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"; 21 request.ContentType = "application/x-www-form-urlencoded"; 22 request.KeepAlive = true; 23 request.Headers.Add("Accept-Encoding", "gzip, deflate"); 24 byte[] data = Encoding.GetEncoding("GB2312").GetBytes(postData); 25 request.ContentLength = data.Length; 26 MessageBox.Show(Convert .ToString(data.Length)); 27 Stream stream = request.GetRequestStream(); 28 stream.Write(data, 0, data.Length); 29 stream.Close(); 30 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 31 StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("GB2312")); 32 string s = sr.ReadToEnd(); 33 response.Close(); 34 return s; 35 }
http://q.cnblogs.com/q/35366/
主要是我想把postData里的用户名和密码传过去完成登陆,可是我的代码怎么也没法登陆
我们学校的登陆系统是根据ip地址记录状态的半小时不使用就释放,所以不需要些cookie
@无边华幕: 判斷是否登錄,不需要cooike?怎麼記錄用戶呢?
@無限遐想: 你 加一下,試一下。
@無限遐想: 我果真是小白,后来我禁用了浏览器的cookie结果就显示未登录了,谢谢你了~
不知道你有什么问题。这里,STREAM.CLOSE不应该调用。
不调用依然登不进去,这可能是哪的问题,最后return了url对应的html代码
@无边华幕: 抓下包,看下你的数据提交是否正确。
@笨笨蜗牛: 我已经抓过了,正常登陆的话,传递的就是我现在postData中的数据,我都快崩溃了,就是找不到问题....