Answered by:
Setting HttpWebRequest User-Agent

Question
-
Hi,
I'm trying to set the User-Agent header of an HttpWebRequest but it looks like I can't:
The UserAgent property no longer exists in WinRT/Metro (Win8 Consumer Preview build 8250, VS11.0.502141), so I tried setting the header directly:
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url); webRequest.Headers[HttpRequestHeader.UserAgent] = "CustomUserAgent"
But this fails with the following exception: "The 'User-Agent' header must be modified using the appropriate property or method."
I also tried using the string "User-Agent" as key for the headers property but the same exception is thrown.
Am I doing something wrong here? Is there a solution/workaround to this issue?
Thanks!
Monday, March 12, 2012 7:25 AM
Answers
-
Hi D,
You can set the UserAgent header using code similar to this:
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://msdn.microsoft.com/");
request.Headers.Date = DateTime.Now.Subtract(new TimeSpan(10,0, 0));
request.Headers.UserAgent.ParseAdd("New User Agent Value");
HttpResponseMessage response = await client.SendAsync(request);
string resultCode = response.StatusCode.ToString();-Jeff
Jeff Sanders (MSFT)
- Marked as answer by Jeff SandersMicrosoft employee, Moderator Wednesday, March 14, 2012 6:20 PM
Wednesday, March 14, 2012 6:20 PMModerator
All replies
-
Hi,
Please check HttpWebRequest.Headers and refer to the following remarks.
The Headers collection contains the protocol headers associated with the request. The following table lists the HTTP headers that are not stored in the Headers collection but are either set by the system or set by properties or methods.
......
User-Agent Set by the UserAgent property.It means that you should set User-Agent via setting property instead of Headers collection.
But this property is not available for Metro Style App in Win8 Consumer Preview.
Actually, HttpWebRequest doesn't use browser to send request. For example, IE is not involved. HttpWebRequest is a complete implementation of an http request using raw TCP in .NET. In the meantime, it's optional in sever side. So you need not to worry about browser issues.
Best wishes,
Robin [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Edited by Robin_YangModerator Tuesday, March 13, 2012 3:54 AM
- Marked as answer by Matt SmallMicrosoft employee, Moderator Wednesday, March 14, 2012 3:28 PM
- Unmarked as answer by Jeff SandersMicrosoft employee, Moderator Wednesday, March 14, 2012 6:19 PM
Tuesday, March 13, 2012 3:51 AMModerator -
Hi D,
You can set the UserAgent header using code similar to this:
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://msdn.microsoft.com/");
request.Headers.Date = DateTime.Now.Subtract(new TimeSpan(10,0, 0));
request.Headers.UserAgent.ParseAdd("New User Agent Value");
HttpResponseMessage response = await client.SendAsync(request);
string resultCode = response.StatusCode.ToString();-Jeff
Jeff Sanders (MSFT)
- Marked as answer by Jeff SandersMicrosoft employee, Moderator Wednesday, March 14, 2012 6:20 PM
Wednesday, March 14, 2012 6:20 PMModerator -
Hi,
Is there any way to set in "User-Agent" header arbitrary text, not as defined in RFC2616?Thanks!
Thursday, July 19, 2012 2:41 PM -
I do not understand what you are asking. Can you provide a concrete example of what you want?
Jeff Sanders (MSFT)
Thursday, July 19, 2012 2:44 PMModerator -
OK, maybe I wasn't clear enough. I'm sorry.
According to RFC2616, "User-agent" header must contain "1*( product | comment )" RFC2616. And object model of System.Net.Http.Headers.HttpHeaders forces to adhere to this rule (grammar). I just want to set in "User-agent" header arbitrary text:
HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("PROPFIND"),
new Uri("https://webdav.server.com/", UriKind.Absolute));
request.Headers.Add("User-Agent", "Arbitrary text \"not submitting\" to {RFC2616}");And in run-time I have an exception:
"An exception of type 'System.FormatException' occurred in System.Net.Http.dll but was not handled in user code
Is any way to bypass these constraints?
Additional information: The format of value '"not submitting" to {RFC2616}' is invalid."Thursday, July 19, 2012 3:32 PM -
Hi Serg,
No you cannot.
-Jeff
Jeff Sanders (MSFT)
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Thursday, July 19, 2012 3:42 PM
Thursday, July 19, 2012 3:42 PMModerator -
Pardon the thread resurrection, but why are you subtracting a time offset from the request header date value?
-- roschler
Saturday, January 25, 2014 4:29 AM