日期:2014-05-19  浏览次数:21485 次

请问HttpWebRequest如何设置Accept-Language和Accept-Encoding?
我发了一个HttpWebRequest给某个网站,可是它却仅仅回复我200   OK,却没有回复任何html代码;而用浏览器访问这个URL却是有页面html的。所以我怀疑是我的C#程序发的HttpWebRequest没有设置Accept-Language和Accept-Encoding的缘故。

请高手帮忙,衷心感谢!

------解决方案--------------------
防抓取了
------解决方案--------------------
网址和你的代码贴出来看看
------解决方案--------------------
用这个吧

private string GetHtmlSource(string url)
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1) Web-Sniffer/1.0.24 ";
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream resStream = response.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(resStream, System.Text.Encoding.UTF8);
string htmlSource = (sr.ReadToEnd());
resStream.Close();
sr.Close();
return htmlSource;
}

测试
richTextBox2.Text = GetHtmlSource( "http://aro999.com ");

输出
<html>
<head>
<title> Welcome Tai Zan </title>
<meta http-equiv= "Content-Type " content= "text/html; charset=UTF-8 ">
</head>
<frameset rows= "*,0 " frameborder= "NO " border= "0 " framespacing= "0 ">
<frame name= "mem_index " src= "http://aro999.com/app/member/ ">
<frame name= "act " scrolling= "NO " noresize src= " ">
</frameset>
<noframes>
<body bgcolor= "#FFFFFF " text= "#000000 ">
</body>
</noframes>
</html>