AndreiC AndreiC - 4 months ago 118
C# Question

Setting the User-Agent header for a WebClient request

What is the proper way of setting the User-Agent header for a WebClient request for Windows Phone 7?
I found 2 options, but not sure which one is the correct one. Considering a WebClient object:

WebClient client = new WebClient();

I saw 2 options:

  1. set the User-Agent using:

    client.Headers["User-Agent"] = "myUserAgentString";

  2. set the User-Agent using the WebHeaderCollection:

    WebHeaderCollection headers = new WebHeaderCollection();
    headers[HttpRequestHeader.UserAgent] = "userAgentString";
    client.Headers = headers;

Can you please advise which of the 2 methods above is the proper one?


you can go here(msdn) for show a sample "proper" for add a user-agent in your WebClient for C# and Here(msdn) for show sample for Windows phone.

this is this sample for c# :

WebClient client = new WebClient ();

// Add a user agent header in case the 
// requested URI contains a query.

client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

this is a sample for Windows Phone (silverlight):

request.Headers["UserAgent"]= "appname";


request.UserAgent = "appname";