【问题标题】:LINQ to XML User-Agent header valueLINQ to XML 用户代理标头值
【发布时间】:2013-10-27 14:34:35
【问题描述】:

当我调用XElement.Load(url) 时,如何为 LINQ to XML 指定 HTTP User-Agent 标头以用于其请求?

我用于对 Web API 的调用,并且要求我的客户端在 User-Agent 标头中正确描述自己。

【问题讨论】:

    标签: c# linq http-headers linq-to-xml user-agent


    【解决方案1】:

    您可以使用 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));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多