调用webrequest或webclient时必须发送哪个HTTP头?

时间:2011-09-14 04:23:34

标签: c# httpwebrequest web-crawler webclient webrequest

我正在创建一个网络机器人。通常,http工具会返回相当多的信息,其中一些是只读的(例如Connect:keep-alive)。如何知道哪些是必需的?

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset: ISO-8859-9,utf-8;q=0.7,*;q=0.3
Accept-Encoding: gzip,deflate,sdch
Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control: max-age=0
Content-Length: 269
Content-Type: application/x-www-form-urlencoded
Host: closure-compiler.appspot.com
Origin: null
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko)       Chrome/14.0.794.0 Safari/535.1

通常代码如下所示。有人指出以下代码错过了设置Content-Type?

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://closure-compiler.appspot.com/compile");
req.Connection = "keep-alive";
req.Headers.Add("Cache-Control", "max-age=0");
req.Headers.Add("Origin","null");
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.794.0 Safari/535.1";
req.ContentType = "application/x-www-form-urlencoded";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
req.Headers.Add("Accept-Language", "tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4");
req.Headers.Add("Accept-Charset", " ISO-8859-9,utf-8;q=0.7,*;q=0.3");
req.Method = "POST";

Stream reqStr = req.GetRequestStream(); 

2 个答案:

答案 0 :(得分:0)

一般请求不需要标头。特定资源可能需要不同的标题。正确的方法是向资源所有者询问需要哪些标头。但如果你想在某种游戏/论坛中作弊,你必须自己弄清楚标题和其他价值。

答案 1 :(得分:0)

根据w3.org的说法,最简单的请求看起来应该与此类似:

GET <uri> CrLf

是的。