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:
set the User-Agent using:
client.Headers["User-Agent"] = "myUserAgentString";
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?
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.
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.
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.
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.
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With