MBZ
MBZ

Reputation: 27592

Custom User Agent for HttpClient?

can I set a custom User Agent for a HttpClient?

I need to view websites in their mobile form.

Upvotes: 29

Views: 29187

Answers (3)

Mayank
Mayank

Reputation: 8852

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)");

Upvotes: 45

DamienG
DamienG

Reputation: 6665

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.

Upvotes: 52

chue x
chue x

Reputation: 18803

How about

    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Add("User-Agent", "my-user-agent-name");

Upvotes: 3

Related Questions