HttpWebRequest错误403

我是C#的新手,需要从C#中检索url。 大多数情况下它工作正常但在一种情况下它会引发错误。 url如下:http: //whois.afrinic.net/cgi-bin/whois?searchtext = 41.132.178.138

错误是:

url的HTTP请求中的exception: http ://whois.afrinic.net/cgi-bin/whois?searchtext = 41.132.178.138远程服务器返回错误:(403)Forbidden。

我的代码是

void MyFUnction(string url) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.UserAgent = ".NET Framework Test Client"; request.ContentType = "application/x-www-form-urlencoded"; Logger.WriteMyLog("application/x-www-form-urlencoded"); // execute the request HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // we will read data via the response stream Stream resStream = response.GetResponseStream(); string tempString = null; int count = 0; do { // fill the buffer with data count = resStream.Read(buf, 0, buf.Length); // make sure we read some data if (count != 0) { // translate from bytes to ASCII text tempString = Encoding.ASCII.GetString(buf, 0, count); if (httpData == null) httpData = tempString; else httpData += tempString; } } while (count > 0); // any more data to read? } 

删除ContentType行。

 request.ContentType.... 

你没有做表格发布,只检索带有“GET”的页面。

 request.Method = "GET"; //this is the default behavior 

并将Accept属性设置为“text / html”。

 request.Accept = "text/html"; 

  request.Accept =“text / html”; 

它也会起作用。

我不知道他们为什么这样配置,可能会故意劝阻一些机器人。 您可能需要检查他们的服务条款是否可以自动查询其网站。


编辑添加:在这种情况下仍然确信我的答案是正确的,如果我没有设置Accept标头,我每次都可以重现403。 ContentType是多余的,但无害。
在任何情况下,您还需要更改function以正确处理响应,并使用正确的字符编码读取响应:

 void MyFunction(string url) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.UserAgent = ".NET Framework Test Client"; request.Accept = "text/html"; Logger.WriteMyLog("application/x-www-form-urlencoded"); // execute the request using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { // we will read data via the response stream Stream resStream = response.GetResponseStream(); StreamReader streamReader = new StreamReader( resStream, Encoding.GetEncoding(response.CharacterSet) ); httpData = streamReader.ReadToEnd(); } }