Bad Request 400 - protocol error on a valid url - Webclient

前端 未结 2 1841
被撕碎了的回忆
被撕碎了的回忆 2020-12-11 09:28

I am trying to parse this page (http://www.coleparmer.co.uk/Product/Turb_Std_Hach_2100q_Kit/WZ-99900-47) using webclient and am having no luck.

 var client         


        
相关标签:
2条回答
  • 2020-12-11 10:23

    It requires a Header.

    WebClient client = new WebClient();
    client.Headers["Content-Type"] = "application/json;charset=UTF-8";    
    
    0 讨论(0)
  • 2020-12-11 10:32

    The appropriate headers needs to be set.

    try
    {
        string html;
        using (WebClient client = new WebClient())
        {
            client.Headers.Add("Accept-Language", " en-US");
            client.Headers.Add("Accept", " text/html, application/xhtml+xml, */*");
            client.Headers.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
            html = client.DownloadString("http://www.coleparmer.co.uk/Product/Turb_Std_Hach_2100q_Kit/WZ-99900-47");
        }
    }
    catch (WebException ex)
    {
        if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
        {
            var resp = (HttpWebResponse)ex.Response;
            if (resp.StatusCode == HttpStatusCode.NotFound) // HTTP 404
            {
                //Handle it
            }
        }
        //Handle it
    }
    
    0 讨论(0)
提交回复
热议问题