Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom User Agent for HttpClient?

can I set a custom User Agent for a HttpClient?

I need to view websites in their mobile form.

like image 212
MBZ Avatar asked Dec 08 '12 00:12

MBZ


People also ask

How to set 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.

How to add User-Agent in Http request c#?

If you wish to add the User-Agent header to a single HttpRequestMessage , this can be done like this: var httpClient = new HttpClient(); var request = new HttpRequestMessage(HttpMethod.

What is User-Agent in HTTP header?

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 User-Agent in curl?

You can use the -A or --user-agent command-line option to pass your own User-Agent string to Curl. By default, Curl sends its own User-Agent string to the server in the following format: "curl/version. number".


2 Answers

var client = new HttpClient(); client.DefaultRequestHeaders.UserAgent.ParseAdd("MyAgent/1.0"); 

There is also a TryParseAdd if you want to catch bad attempts at adding it and Add if you want to create the ProductInfoHeaderValue with a name and version number yourself.

like image 144
DamienG Avatar answered Oct 13 '22 00:10

DamienG


Here you go

var client = new HttpClient(); client.DefaultRequestHeaders.Add("User-Agent",                                  "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2;                                    WOW64; Trident/6.0)"); 
like image 45
Mayank Avatar answered Oct 12 '22 22:10

Mayank