c# get http://www.zhantai.com/ 网页出先乱码,研究了下应该不是编码问题,大哥大家帮帮忙! myRequest = (HttpWebRequest)WebRequest.Create("http://www.zhantai.com/");
myRequest.Method = "GET"; myRequest.CookieContainer = cookie;
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.Headers.Add("Accept-Language", "zh-cn");
myRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
myRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
myRequest.Referer = "http://www.zhantai.com/";
myRequest.KeepAlive = true;
myRequest.AllowWriteStreamBuffering = true;
myRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
myRequest.MaximumResponseHeadersLength = -1;
using (HttpWebResponse res = (HttpWebResponse)myRequest.GetResponse())
{
using (StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding("gb2312")))
{
result = sr.ReadToEnd();
sr.Close();
}
}
myRequest.Method = "GET"; myRequest.CookieContainer = cookie;
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.Headers.Add("Accept-Language", "zh-cn");
myRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
myRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
myRequest.Referer = "http://www.zhantai.com/";
myRequest.KeepAlive = true;
myRequest.AllowWriteStreamBuffering = true;
myRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
myRequest.MaximumResponseHeadersLength = -1;
using (HttpWebResponse res = (HttpWebResponse)myRequest.GetResponse())
{
using (StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding("gb2312")))
{
result = sr.ReadToEnd();
sr.Close();
}
}
private string GetHtmlCode(string url, Encoding encoding)
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream resStream = response.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(resStream, encoding);
string html = (sr.ReadToEnd());
resStream.Close();
sr.Close();
return html;
}
//调用
richTextBox2.Text = GetHtmlCode("http://www.zhantai.com/", Encoding.GetEncoding("gb2312"));
检查HTTP头,ContentType
/// 得到整个网页的源码
/// </summary>
/// <param name="Url"></param>
/// <returns></returns>
public static string _GetHtml(string Url)
{ Stream MyInStream = null;
string Html = "";
try
{
HttpWebRequest MyRequest = (HttpWebRequest)WebRequest.Create(Url);
HttpWebResponse MyResponse = (HttpWebResponse)MyRequest.GetResponse(); MyInStream = MyResponse.GetResponseStream(); Encoding encode = System.Text.Encoding.UTF8;
StreamReader sr = new StreamReader(MyInStream, encode); Char[] read = new Char[256];
int count = sr.Read(read, 0, 256);
while (count > 0)
{
String str = new String(read, 0, count);
Html += str;
count = sr.Read(read, 0, 256);
}
}
catch (Exception)
{
Html = "错误";
}
finally
{
if (MyInStream != null)
{
MyInStream.Close();
}
}
return Html;
谢谢大家