WebRequest.GetResponse - The remote server returned an error: (404) Not Found

WebRequest.GetResponse - The remote server returned an error: (404) Not Found

我正在尝试连接到一个网站,但它一直返回此错误,即使我可以在我的浏览器中访问该网站:

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

站点希望将用户代理添加到请求中。您可以 google What's my user agent? 找到自己的并像这样添加:

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

你需要添加UserAgent as ,除了他的回答,最好使用HttpWebClient 的AutomaticDecompression 属性 并且你可以添加Accept header。我还在 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