错误的请求400 -协议错误在一个有效的url - web客户端

本文关键字:错误 一个 有效 url web 客户端 请求 协议 | 更新日期: 2023-09-27 17:52:46

我正在尝试解析这个页面(http://www.coleparmer.co.uk/Product/Turb_Std_Hach_2100q_Kit/WZ-99900-47)使用网络客户端和我没有运气。

 var client = new WebClient();
  var html = client.DownloadString("http://www.coleparmer.co.uk/Product/Turb_Std_Hach_2100q_Kit/WZ-99900-47");

错误的请求400 -协议错误在一个有效的url - web客户端

需要设置适当的标头。

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
}

需要一个Header

WebClient client = new WebClient();
client.Headers["Content-Type"] = "application/json;charset=UTF-8";    
相关文章: