编辑:我也不相信HttpListener会做任何事情
我如何运用某些东西给我这个浏览器魔术?
* WebClient没有.RequestHeaders。
*将HttpWebRequest标头与Chrome / Fiddler嗅探进行比较。
using System.Net;
private void Form1_Load(object sender, EventArgs e)
{
CookieContainer cookieJar = new CookieContainer();
cookieJar.GetCookies(new Uri("https://www.google.com"));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.google.com");
request.CookieContainer = cookieJar;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
this.Text = request.Headers.Count.ToString();
WebHeaderCollection header = request.Headers;
for (int i = 0; i < header.Count; i++)
{
richTextBox1.AppendText(header.GetKey(i) + ": " + header[i] + "\n");
}
}
Fiddler / Chrome组合返回10个请求标头;客户返回2.
另外为什么标题“Accept-Encoding:gzip,deflate,sdch”总是让响应变得奇怪2个字符的数据翻转?
答案 0 :(得分:0)
目前还不是很清楚你想要实现什么,但WebClient有一个Headers属性,你可以使用它来使请求标题看起来像你想要的那样:
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.152 Safari/537.22";
client.Headers[HttpRequestHeader.AcceptLanguage] = "fr-FR,fr;q=0.8";
... you could set here whatever headers you want
string result = client.DownloadString("http://www.google.com");
}