gpt4 book ai didi

c# - 错误请求 400 - 有效 URL 上的协议(protocol)错误 - Web 客户端

转载 作者:行者123 更新时间:2023-11-30 13:22:27 26 4
gpt4 key购买 nike

我正在尝试解析此页面 ( http://www.coleparmer.co.uk/Product/Turb_Std_Hach_2100q_Kit/WZ-99900-47 ) 使用 webclient 并没有运气。

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

最佳答案

需要设置适当的 header 。

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
}

关于c# - 错误请求 400 - 有效 URL 上的协议(protocol)错误 - Web 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22623664/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com