Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 773
AndreiC Avatar asked Aug 07 '12 07:08

AndreiC


People also ask

Is user agent a request header?

The HTTP headers User-Agent is a request header that allows a characteristic string that allows network protocol peers to identify the Operating System and Browser of the web-server. Your browser sends the user agent to every website you connect to.

What is a header in WebClient?

The Headers property contains a WebHeaderCollection instance containing protocol headers that the WebClient sends with the request. Some common headers are considered restricted and are protected by the system and cannot be set or changed in a WebHeaderCollection object.

What does the user agent request header contains?

The User-Agent request header is a characteristic string that lets servers and network peers identify the application, operating system, vendor, and/or version of the requesting user agent.

How do I change the user agent in HTTP request?

To set a custom user agent, include an agent string in the HTTP header User-Agent. For the integration name, use a string that clearly and meaningfully identifies your integration. For the integration version, use a build ID, commit hash, or other identifier that is updated when you release new integration versions.


1 Answers

You can check the WebClient documentation for a C# sample that adds a User-Agent to your WebClient and here for a sample for Windows Phone.

This is the 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"; // OR request.UserAgent = "appname"; 
like image 200
Doc Roms Avatar answered Sep 19 '22 00:09

Doc Roms