2017-09-07 134 views
0

我試圖連接到一個網站,但它一直返回該錯誤,即使我可以在我的瀏覽器訪問網站:WebRequest.GetResponse - 遠程服務器返回錯誤:(404)未找到

An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code

Additional information: The remote server returned an error: (404) Not Found.

我很確定我的代碼是正確的,因爲我最近使用了相同的代碼,但不能解決它爲什麼返回一個錯誤,有什麼建議嗎? 我的代碼:

OddsTodayREQUEST = WebRequest.Create("http://www.betexplorer.com/next/soccer/") 
Using OddsTodayRESPONSE As WebResponse = OddsTodayREQUEST.GetResponse() 
      Using OddsTodayREADER As New StreamReader(OddsTodayRESPONSE.GetResponseStream()) 
       OddsTodayHTML = OddsTodayREADER.ReadToEnd() 
      End Using 
     End Using 
+0

404 =頁面不存在。你能在瀏覽器中點擊該網站上的確切頁面嗎? – GibralterTop

+0

是的,我可以在我的瀏覽器中找到它 – HitmanHeathcote

回答

0

您需要添加的UserAgent爲@ChaseRocker提到的,除了他的答案,這是更好地使用HttpWebClient的AutomaticDecompression財產,您可以添加接受頭。我還在Using聲明中使用了OddsTodayRESPONSE.GetResponseStream()

Dim OddsTodayREQUEST As HttpWebRequest = WebRequest.Create("http://www.betexplorer.com/next/soccer/") 
OddsTodayREQUEST.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" 
OddsTodayREQUEST.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate 'Decompressing makes the request be done faster 
OddsTodayREQUEST.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0" 
Using OddsTodayRESPONSE As HttpWebResponse = OddsTodayREQUEST.GetResponse() 
    Using OddsTodayRESPONSESTREAM = OddsTodayRESPONSE.GetResponseStream() 
     Using OddsTodayREADER As New StreamReader(OddsTodayRESPONSESTREAM) 
      OddsTodayHTML = OddsTodayREADER.ReadToEnd() 
     End Using 
    End Using 
End Using 
+0

非常感謝您爲此解釋,現在完美地工作。 – HitmanHeathcote

1

該網站希望用戶代理添加到請求。你可以在google What's my user agent?找到自己,並添加這樣的:

OddsTodayREQUEST.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)" 
+0

非常感謝你幫助我,最好是包括一個用戶代理來抓取所有這樣的網站或只需要一個用戶代理的網站?如果是後者,你怎麼知道網站需要一個? – HitmanHeathcote

+0

如果你想複製瀏覽器的功能,我建議你加入用戶代理。此外,我只能確定該網站需要通過測試。我得到了同樣的404錯誤,所以我添加了一個用戶代理,它工作。 –

+0

真棒,感謝您抽出時間,不適合將其包含在所有未來的項目中 – HitmanHeathcote

相關問題