[英]Getting Error “The remote server returned an error: (403) Forbidden” when screen scraping using HttpWebRequest.GetResponse()
我們有一個工具可以檢查給定的URL是否是實時URL。 如果給定的網址是實時的,我們軟件的另一部分可以屏蔽其中的內容。
這是我檢查網址是否有效的代碼
public static bool IsLiveUrl(string url)
{
HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
webRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5";
webRequest.CookieContainer = new CookieContainer();
WebResponse webResponse;
try
{
webResponse = webRequest.GetResponse();
}
catch (WebException e)
{
return false;
}
catch (Exception ex)
{
return false;
}
return true;
}
這段代碼完美無缺,但是對於在apache上托管的特定站點,我收到了一個帶有以下消息的Web異常。 “遠程服務器返回錯誤:(403)Forbidden”在進一步檢查時,我在WebException對象中找到了以下詳細信息
Status =“ProtocolError”StatusDescription =“不良行為”
這是請求標題“User-Agent:Mozilla / 5.0(Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6)Gecko / 20060728 Firefox / 1.5主機:scenicspares.co.uk連接:Keep-Alive “
這是響應標題“Keep-Alive:timeout = 4,max = 512 Connection:Keep-Alive Transfer-Encoding:chunked Content-Type:text / html Date:Thu,13 Jan 2011 10:29:36 GMT Server:Apache “
我在vs2008中使用手表提取了這些標題。 使用的框架工作是3.5。
事實證明,我需要做的就是遵循
webRequest.Accept = "*/*";
webResponse = webRequest.GetResponse();
它是固定的。
我相信有很多類似的問題依賴於服務器應用程序。 在我的特定情況下,請參閱: 遠程服務器返回錯誤:(403)禁止
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.