Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# HttpClient adding "User-Agent" header shows up as several different headers

Tags:

c#

asp.net

When adding a "User-Agent" header to HttpClient it shows up as several User-Agent headers instead in the request. It seems as the string added as User-Agent breaks upon a space character by default and then they are added as separate User-Agents. How can I add a single User-Agent string with spaces using HttpClient?

var cookieContainer = new CookieContainer();
var handler = new HttpClientHandler();
handler.CookieContainer = cookieContainer;
var httpClient = new HttpClient(handler);
httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36");
//Did not work either, same result
//httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36");
//httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36");

enter image description here

enter image description here

like image 316
Ogglas Avatar asked Apr 04 '17 19:04

Ogglas


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


1 Answers

You should not worry about that. It splits your user agent into several parts internally, because that is how User-Agent header defined in RFC:

The User-Agent request-header field contains information about the
user agent originating the request. This is for statistical purposes, the tracing of protocol violations, and automated recognition of user agents for the sake of tailoring responses to avoid particular user
agent limitations. User agents SHOULD include this field with
requests. The field can contain multiple product tokens (section 3.8) and comments identifying the agent and any subproducts which form a
significant part of the user agent. By convention, the product tokens are listed in order of their significance for identifying the
application.

User-Agent = "User-Agent" ":" 1*( product | comment )

So what you see are those "product tokens" and if you explore each - you will see that they have Product and Comment properties.

However, this does not mean it will send this as 6 headers. It will send one User-Agent header, same as you provided as string.

like image 164
Evk Avatar answered Sep 20 '22 17:09

Evk