在调用XElement.Load(url)时,如何为LINQ指定用于其请求的header报头
我使用对Web的调用,它是必需的,我的客户端必须在用户代理头中正确地描述自己。
发布于 2013-10-27 18:13:19
可以使用WebClient指定用户代理。
using (var webClient = new WebClient())
{
webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
using (var stream = webClient.OpenRead("http://server.com"))
{
XElement.Load(stream);
}
}或
using (var webClient = new WebClient())
{
webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
XElement.Parse(webClient.DownloadString(url));
}https://stackoverflow.com/questions/19619218
复制相似问题