Reputation: 117
I'm trying to set the user-agent in my http header in our override function GetWebRequest in C#
protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
System.Net.HttpWebRequest request = base.GetWebRequest(uri) as System.Net.HttpWebRequest;
request.Headers.Add("User-Agent", (someone@yahoo.com"));
request.ProtocolVersion = System.Net.HttpVersion.Version10;
request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
request.KeepAlive = false;
return request;
}
My request is being returned with an error and I was told they are not seeing the user-agent. I've tried other ways of setting the header user-agent but nothing is working so far. Some other ways I've tried are
request.Headers["User-Agent"] = "someone@yahoo.com");
request.UserAgent = "someone@yahoo.com";
request.Headers.Set("User-Agent", "someone@yahoo.com");
The site I am trying to send my request is telling me it needs a way to identify us in case of issues and "please make sure your request includes the "user-agent" http header." They've sent me an example but it is using CURL
curl_setopt($ch, CURLOPT_USERAGENT,'jeremy.fields@gmail.com)
Upvotes: 10
Views: 40102
Reputation: 623
If you write your own application requesting http content it is best practice to set any form of identification in the user agent. It can be email, website or product name if that is widely known.
According to HTTP specification the product tokens in the user agent:
(...) SHOULD be short and to the point. They MUST NOT be used for advertising or other non-essential information. Although any token character MAY appear in a product-version, this token SHOULD only be used for a version identifier (i.e., successive versions of the same product SHOULD only differ in the product-version portion of the product value).
In its easiest form it can be one of the following:
request.UserAgent = "MyAppName/1.0.0";
request.UserAgent = "MyAppName/1.0.0 (someone@example.com)";
request.UserAgent = "MyAppName/1.0.0 (+http://www.example.com)";
URLs in comment must be prefixed with +
(documented in this answer by Brendon). Also worth noticing that any mail address should go to company or developer responsible for the product, not to the user using it.
If you need compatibility with modern browsers (if you expect the result to be HTML, and not an API with xml/json/etc.), you can also add the "default" Mozilla version along with your operating system info:
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) MyAppName/1.0.0 (someone@example.com)"
It is best to send correct system info, and not just copy a found user agent string with incorrect operating system info.
Upvotes: 2
Reputation: 307
User-agent represent your browser like Firefox or Chrome
Use the UserAgent property on HttpWebRequest by casting it to a HttpWebRequest
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = "my user agent";
or
request = new HttpClient();
request.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0");
Upvotes: 4
Reputation: 31
I think you are using the User-Agent header incorrectly. From MSDN:
When you visit a webpage, your browser sends the user-agent string to the server hosting the site that you are visiting. This string indicates which browser you're using, its version number, and details about your system, such as operating system and version. The web server can use this information to provide content that is tailored for your specific browser.
An example from that page:
User-Agent: Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko
Upvotes: 0