Skip to content

Can Not Add Header To HttpClient #1618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
hamiddd1980 opened this issue Mar 27, 2025 · 8 comments · May be fixed by nanoframework/System.Net.Http#477
Open

Can Not Add Header To HttpClient #1618

hamiddd1980 opened this issue Mar 27, 2025 · 8 comments · May be fixed by nanoframework/System.Net.Http#477

Comments

@hamiddd1980
Copy link

Library/API/IoT binding

nanoFramework.System.Net.Http

Visual Studio version

Microsoft Visual Studio Enterprise 2022 (64-bit) - Preview Version 17.14.0 Preview 2.0

.NET nanoFramework extension version

2022.12.136

Target name(s)

ESP32-S3

Firmware version

No response

Device capabilities

Target capabilities:
Has nanoBooter: NO
IFU capable: NO
Has proprietary bootloader: YES

Description

It is not possible to add Http headers (like User-Agent, Accept) to HttpClient by DefaultRequestHeaders or HttpRequestMessage . Trying to add standard Headers, produces the following exception:
Cannot update restricted header User-Agent.
Cannot update restricted header Accept.

Inspecting the online source code reveals that the problem arises from WebHeaderCollection class in ThrowOnRestrictedHeader method:
the class has a property named HInfo with type of HeaderInfoTable . HeaderInfoTable has a table with predefined Http Headers (Like User-Agent) .
User can not add a Http Header if that header exists in the HeaderInfoTable.
So, how can I add (or change) standard Http Header to httpClient (Like User-Agent,Accept)?

How to reproduce

1.Create a Static HttpClient
httpClient = new HttpClient();
2.Try to Add Headers To DefaultRequestHeaders

httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
httpClient.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.5");
 httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:136.0) Gecko/20100101 Firefox/136.0");

Expected behaviour

It should be possible to Add or Change Standard Http Headers to HttpClient

Screenshots

No response

Sample project or code

No response

Aditional information

No response

@Ellerbach
Copy link
Member

As far as I remember, it's the same behavior on real .NET .Just try to add a header normally, it should just override the default one.

@hamiddd1980
Copy link
Author

I'm sure it is possible to add Headers(standard or user defined) to HttpClient in .net Core.Indeed, many servers doesn't accept Http request in case the headers are empty.

@hamiddd1980
Copy link
Author

Hi, is there any one here to help me?
Thanks in advance for your helps with this issue.

@hamiddd1980
Copy link
Author

I think the following line from HttpHeaders class should be corrected (change boolean value from true to false):

public abstract class HttpHeaders
    {
       internal WebHeaderCollection _headerStore = new WebHeaderCollection(true); //we should change true param to false
       //the first parameter is m_IsHttpWebHeaderObject and according to comments for this property in WebHeaderCollection class, it should be TRUE if the object is created for internal use
       

        /// <summary>
        /// true if this object is created for internal use, in this case
        /// we turn on checking when adding special headers.
        /// </summary>
        private bool m_IsHttpWebHeaderObject = false;

@josesimoes
Copy link
Member

@hamiddd1980 if you do that change can you add/modify the HTTP headers collection as you intend?

@hamiddd1980
Copy link
Author

@josesimoes , I tested the above method and regarding the results below it seems this modification can solve the problem .

1-before modification :
1-1)if you try to add headers to Httpclient, you'll face the Exception :

Image

1-2)and if you don't add headers , you don't have headers on server side:

Image

2-after code modification :
2-1)you can include headers in Httpclient without problem:

Image

2-2)on the server side,you receive these headers :

Image

I'm not familiar with Github, so it would be grateful if you could apply this Modification .
Thank you.

@josesimoes
Copy link
Member

Hi @hamiddd1980!
Thank you for the detailed investigation. Adding this to the work queue.

It's quite easy to do this: you can edit the file right on github and it will offer to open a pull request for you. No need to install anything, not clone locally.

networkfusion added a commit to nanoframework/System.Net.Http that referenced this issue Apr 12, 2025
@networkfusion networkfusion linked a pull request Apr 12, 2025 that will close this issue
14 tasks
@networkfusion
Copy link
Member

@hamiddd1980, please let me know if you can test nanoframework/System.Net.Http#477 (and what you expected it to be).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants