我是C#中的新成員,並且需要從C#中檢索URL。大多數情況下它工作正常,但在一種情況下,它會拋出一個錯誤。 URL是如下 http://whois.afrinic.net/cgi-bin/whois?searchtext=41.132.178.138HttpWebRequest錯誤403
錯誤是:禁止(403):
異常在對URL的HTTP請求:http://whois.afrinic.net/cgi-bin/whois?searchtext=41.132.178.138遠程服務器返回一個錯誤。
我的代碼
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?
}
格式化你的代碼,在編輯器中選擇它,按下Control-K。 – 2010-07-12 19:42:09