实在搞不定了,谁能帮我用HttpWebRequest实现登录这个网站进行操作。
地址:http://222.216.28.167:10011/index.html
账号:26488685
密码:123456
登录后在“服务器管理”里进行“停止”或“开始”操作
我现在用的代码:
===============================================
// 登录
System.Net.CookieContainer mcc = PostLogin();
// 模似 "停止 "按钮
this.tbReturnHtml.Text = GETCommand( "http://222.216.28.167:10011/stop_uvs.uscmd?ID=460474 ", mcc);
============================================================
public static CookieContainer PostLogin()
{
string url = "http://222.216.28.167:10011/login.uscmd ";
string args = "username=26488685&password=123456 ";
string outdata = " ";
Encoding encoding = Encoding.GetEncoding( "gb2312 ");
CookieCollection myCookies = null;
CookieContainer myCookieContainer = new CookieContainer();
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */* ";
myHttpWebRequest.Referer = "http://222.216.28.167:10011/ ";
myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727) ";
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded ";
myHttpWebRequest.Method = "POST ";
myHttpWebRequest.CookieContainer = myCookieContainer;
// 写入
byte[] b = encoding.GetBytes(args);
myHttpWebRequest.ContentLength = b.Length;
using (System.IO.Stream sw = myHttpWebRequest.GetRequestStream())
{
sw.Write(b, 0, b.Length);
if (sw != null)
sw.Close();
}
HttpWebResponse response = null;
System.IO.StreamReader sr = null;
response = (HttpWebResponse)myHttpWebRequest.GetResponse();
myCookies = response.Cookies;
sr = new System.IO.StreamReader(response.GetResponseStream(), encoding);
outdata = sr.ReadToEnd();
return myCookieContainer;
}
-------------------------------
public static string GETCommand(string url, CookieContainer myCookieContainer)
{
string outdata = " ";
Encoding encoding = Encoding.GetEncoding( "gb2312 ");
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.ContentType = "text/html ";
myHttpWebRequest.Method = "GET ";
myHttpWebRequest.CookieContainer = myCookieContainer;
HttpWebResponse response = null;
System.IO.StreamReader sr = null;
response = (HttpWebResponse)myHttpWebRequest.GetResponse();
sr = new System.IO.StreamReader(response.GetResponseStream(), encoding);
outdata = sr.ReadToEnd();
return outdata;
}
======================================================
查看POST登录数据后返回的数据是正常的,是登录后服务器转到的页面,
但之后再 http://222.216.28.167:10011/stop_uvs.uscmd?ID=460474 就会转到登录页面,说明没有能保持登录状态。
登录成功后转到的页面 http://222.216.28.167:10011/main.html 里有一段 javascript 代码好像是保存cookie的,不知道是不是跟这个有关,高手们帮我看看,谢谢了
[解决办法]
帮你顶,我也想知道
[解决办法]
UP
[解决办法]
string args = "username=26488685&password=123456 ";
===========================================
怀疑是这个,,POST过去的时候抓抱看看是什么数据..再替换
string args = "username=26488685&password=123456 ";
[解决办法]
如果它有JAVASCRIPT 来判断LOGIN
那就麻烦了 建议用WEBBROWSER 控件
[解决办法]
如果不是
加这个呢?
myHttpWebRequest.AllowAutoRedirect = true;
[解决办法]
应该和Cookie有关,检查哪里创建Cookie的,哪里判断Cookie的