问题描述
publicstringGetHtml(stringurl,stringpostData,boolisPost,CookieContainercookieContainer){if(string.IsNullOrEmpty(postData)){returnGetHtml(url,cookieContainer);}Thread.Sleep(delay);currentTry++;try{byte[]byteRequest=Encoding.Default.GetBytes(postData);HttpWebRequesthttpWebRequest;httpWebRequest=(HttpWebRequest)HttpWebRequest.Create(url);httpWebRequest.CookieContainer=cookieContainer;httpWebRequest.ContentType=contentType;httpWebRequest.Referer=url;httpWebRequest.Accept=accept;httpWebRequest.UserAgent=userAgent;httpWebRequest.Method=isPost?"POST":"GET";httpWebRequest.ContentLength=byteRequest.Length;encode=Encoding.GetEncoding("GB2312");Streamstream=httpWebRequest.GetRequestStream();stream.Write(byteRequest,0,byteRequest.Length);stream.Close();HttpWebResponsehttpWebResponse;httpWebResponse=(HttpWebResponse)httpWebRequest.GetResponse();StreamresponseStream=httpWebResponse.GetResponseStream();StreamReaderstreamReader=newStreamReader(responseStream,encoding);stringhtml=streamReader.ReadToEnd();streamReader.Close();responseStream.Close();currentTry=0;returnhtml;}catch(Exceptione){Console.ForegroundColor=ConsoleColor.Red;Console.WriteLine(DateTime.Now.ToString("HH:mm:ss")+e.Message);Console.ForegroundColor=ConsoleColor.White;if(currentTry<=maxTry){GetHtml(url,postData,isPost,cookieContainer);}currentTry=0;returnstring.Empty;}}publicstaticstringLogin(stringloginEmail,stringloginPassword){stringloginUrl="http://www.222idc.com/user/userlogin.asp";stringpostData=string.Format("username={0}&password={1}",loginEmail,loginPassword);stringresult=httpHelper.GetHtml(loginUrl,postData,true,cookieContainer);returnresult;}HttpWebRequest读取网站返回为中文成乱码请指导员上以是我使用的代码,还是一个登录加cookieS记录的.Login为登录事件,可是返回result里的中文全是乱码了.请大虾修改指导员.
解决方案
解决方案二:
用Fiddler抓包看看,有可能返回的是Gzip压缩流。
解决方案三:
把StreamReaderstreamReader=newStreamReader(responseStream,encoding);这句的编码换成utf-8试试,如不行,就是如1楼所说的那样。
解决方案四:
问题解决,非常感谢楼上二位,是我的自己粗心了.问题出在encoding这个变量上.我设置错了.
解决方案五:
最近也在研究这个东西,想用WEB的去保存别的网站的登录操作怎么做?