各位高手,我在用httpwebrequest模拟登录中国专家网博客的时候,看似很简单,却不知道为何无论如何都登陆不了,弄了半天也没解决,故在这里向各位请教了,我的代码如下:没发现什么问题,却老是报 远程服务器(500)内部错误,希望有高手能帮我找出问题,感激不尽,100分相送! CookieContainer cookies = new CookieContainer();
// //以下是登陆中国网专家博客 登录成功
HttpWebRequest logreq = (HttpWebRequest)WebRequest.Create("http://blog.china.com.cn/user/login.do");
string Loginpostdata = "returnUrl=%2Fsite%2FloginChinaBlog.jsp&userName=" + username + "&pwd=" + password ; byte[] postdata = Encoding.UTF8.GetBytes(Loginpostdata);
logreq.ServicePoint.Expect100Continue = false;
//byte[] postdata = Encoding.Default.GetBytes(Loginpostdata);
ServicePointManager.Expect100Continue = false;
logreq.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E)";
//模拟一个UserAgent
logreq.CookieContainer = cookies;
logreq.Method = "POST";
//logreq.AllowAutoRedirect = true;
logreq.KeepAlive = true;
logreq.ContentLength = postdata.Length;
logreq.Headers.Add("Accept-Language: zh-cn");
logreq.Headers.Add("Accept-Encoding: gzip, deflate");
logreq.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, application/QVOD, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, */*";
logreq.Referer = "http://blog.china.com.cn/site/loginChinaBlog.jsp";
logreq.ContentType = "application/x-www-form-urlencoded";
logreq.GetRequestStream().Write(postdata, 0, postdata.Length);
logreq.GetRequestStream().Close();
//logreq.GetResponse();
HttpWebResponse response = (HttpWebResponse)logreq.GetResponse();
cookies.Add(response.Cookies);
StreamReader sr2 = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("GB2312"));
string text2 = sr2.ReadToEnd();
return cookies;
// //以下是登陆中国网专家博客 登录成功
HttpWebRequest logreq = (HttpWebRequest)WebRequest.Create("http://blog.china.com.cn/user/login.do");
string Loginpostdata = "returnUrl=%2Fsite%2FloginChinaBlog.jsp&userName=" + username + "&pwd=" + password ; byte[] postdata = Encoding.UTF8.GetBytes(Loginpostdata);
logreq.ServicePoint.Expect100Continue = false;
//byte[] postdata = Encoding.Default.GetBytes(Loginpostdata);
ServicePointManager.Expect100Continue = false;
logreq.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E)";
//模拟一个UserAgent
logreq.CookieContainer = cookies;
logreq.Method = "POST";
//logreq.AllowAutoRedirect = true;
logreq.KeepAlive = true;
logreq.ContentLength = postdata.Length;
logreq.Headers.Add("Accept-Language: zh-cn");
logreq.Headers.Add("Accept-Encoding: gzip, deflate");
logreq.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, application/QVOD, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, */*";
logreq.Referer = "http://blog.china.com.cn/site/loginChinaBlog.jsp";
logreq.ContentType = "application/x-www-form-urlencoded";
logreq.GetRequestStream().Write(postdata, 0, postdata.Length);
logreq.GetRequestStream().Close();
//logreq.GetResponse();
HttpWebResponse response = (HttpWebResponse)logreq.GetResponse();
cookies.Add(response.Cookies);
StreamReader sr2 = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("GB2312"));
string text2 = sr2.ReadToEnd();
return cookies;
你看看text2 提示什么?是不是要验证码?
就报 : 远程服务器返回错误: (500) 内部服务器错误
根本无法执行,我通过抓包分析,跟IE访问一样的http头了,期待有高手能解决看看,另外,是否有高手能模拟登录天涯论坛以及西祠胡同论坛?最近在研究这一块,希望高手不辞令教!
Host: blog.china.com.cn
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.25 Safari/532.5
Referer: http://blog.china.com.cn/site/loginChinaBlog.jsp
Content-Length: 64
Cache-Control: max-age=0
Origin: http://blog.china.com.cn
Content-Type: application/x-www-form-urlencoded
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Encoding: gzip,deflate
Cookie: __utmz=80566853.1260328332.1.1.utmcsr=blog.china.com.cn|utmccn=(referral)|utmcmd=referral|utmcct=/; __utma=80566853.353211925.1260328332.1260328332.1260328332.1; __utmc=80566853; __utmb=80566853.1.10.1260328332; JSESSIONID=DB7C45B8386E97ED9E5AF01C8A88241C.worker3; com.trs.idm.gSessionId=01387F6BA3C3EC4602C12781B2B72924; com.trs.ids.guest=1
Accept-Language: zh-CN,zh;q=0.8
Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3returnUrl=%2Fsite%2FloginChinaBlog.jsp&userName=zcl24&pwd=****
但就是不能成功?你登陆成功了?