Question

How can I specify HTTP User-Agent header for LINQ to XML to use for its requests when I call XElement.Load(url)?

I use for calls to Web API and it's required, that my client describes itself properly in User-Agent header.

Was it helpful?

Solution

You could use WebClient for specify user agent

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);
    }
}

or

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));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top