Unable to retrieve HTML via HttpWebRequest

I am trying to parse the HTML code of the http://odds.bestbetting.com/horse-racing/today page to have a list of races, etc. The problem is that I cannot get the HTML code of the page. Here is the C # function code:

    public static string Http(string url) {          
            Uri myUri = new Uri(url);
            // Create a 'HttpWebRequest' object for the specified url. 
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(myUri);
            myHttpWebRequest.AllowAutoRedirect = true;
            // Send the request and wait for response.
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            var stream = myHttpWebResponse.GetResponseStream();
            var reader = new StreamReader(stream);
            var html = reader.ReadToEnd();
            // Release resources of response object.
            myHttpWebResponse.Close();

            return html;
    }

When I run the program calling the function, it throws an exception in

HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse ();

which the:

Unable to handle forwarding from HTTP / HTTPS to other dissimilar ones.

, , , . - , , , - . : odds.bestbetting.com/horse-racing/2011-06-10/byCourse   odds.bestbetting.com/horse-racing/2011-06-10/byTime, , .

, - , . ?

!

+3
2

.

- FTP- - .

, , .

, .

+1

- ... , User-Agent.

, , , . , , UserAgent . , , , , ... .

, , :

    public static string Http(string url) {
        if (url.Length > 0)
        {
            Uri myUri = new Uri(url);
            // Create a 'HttpWebRequest' object for the specified url. 
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(myUri);
            // Set the user agent as if we were a web browser
            myHttpWebRequest.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4";

            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            var stream = myHttpWebResponse.GetResponseStream();
            var reader = new StreamReader(stream);
            var html = reader.ReadToEnd();
            // Release resources of response object.
            myHttpWebResponse.Close();

            return html;
        }
        else { return "NO URL"; }
    }

.

+3

Source: https://habr.com/ru/post/1746607/


All Articles