HttpClient Request like browser

前端 未结 4 935
自闭症患者
自闭症患者 2020-12-04 14:59

When I calling site www.livescore.com by HttpClient class I always getting error \"500\". Probably server blocked request from HttpClients.

1)There is any other meth

4条回答
  •  [愿得一人]
    2020-12-04 15:23

    Several things to take note of.

    1. That site requires you to provide a user agent, or it returns a 500 HTTP error.

    2. A GET request to livescore.com responds with a 302 to livescore.us. You need to handle the redirection or directly request livescore.us

    3. You need to decompress a gzip-compressed response

    This code works using the .NET 4 Client Profile, I'll let you figure out if it fits a Windows Store app.

    var request = (HttpWebRequest)HttpWebRequest.Create("http://www.livescore.com");
    request.AllowAutoRedirect = true;
    request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17";
    
    string content;
    
    using (var response = (HttpWebResponse)request.GetResponse())
    using (var decompressedStream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
    using (var streamReader = new StreamReader(decompressedStream))
    {
        content = streamReader.ReadToEnd();
    }
    

提交回复
热议问题