C # HttpClient adding a "User-Agent" header displays as several different headers

When you add the "User-Agent" HttpClient to the HttpClient it appears as multiple User-Agent headers in the request. It seems that the line has been added since the user agent is split by default space character and then added as a 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

+5
source share
1 answer

You do not have to worry about this. It breaks your user agent into several parts inside, because this is how the User-Agent header in the RFC is defined:

The user agent request header field contains information about the user agent file sending the request. This is done for statistical purposes, tracking protocol violations and automatically recognizing user agents to adapt responses to avoid a specific user
agent restrictions. User agents MUST include this field using Requests. The field may contain several product tokens (section 3.8) and comments identifying the agent and any offal that form a significant part of the user agent. By convention, product tokens are listed in order of importance for identifying the application.

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

So, you see those β€œproduct tokens”, and if you examine them, you will see that they have the Product and Comment properties.

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

+5
source

Source: https://habr.com/ru/post/1266328/


All Articles