I am trying to connect to a website but it keeps returning this error even though i can reach the website in my browser:
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.
I'm pretty sure my code is correct as I've used the same code a lot recently but cannot work out why it returning an error, any suggestions? My Code:
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
You need to add UserAgent as @ChaseRocker mentioned, In addition to his answer, It's better to use AutomaticDecompression property of HttpWebClient and you may add Accept header. I also used OddsTodayRESPONSE.GetResponseStream()
in Using
statement.
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