-
-
Notifications
You must be signed in to change notification settings - Fork 83
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
Can Not Add Header To HttpClient #1618
Comments
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. |
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. |
Hi, is there any one here to help me? |
I think the following line from HttpHeaders class should be corrected (change boolean value from true to false):
|
@hamiddd1980 if you do that change can you add/modify the HTTP headers collection as you intend? |
@josesimoes , I tested the above method and regarding the results below it seems this modification can solve the problem . 1-before modification : 1-2)and if you don't add headers , you don't have headers on server side: 2-after code modification : 2-2)on the server side,you receive these headers : I'm not familiar with Github, so it would be grateful if you could apply this Modification . |
Hi @hamiddd1980! 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. |
@hamiddd1980, please let me know if you can test nanoframework/System.Net.Http#477 (and what you expected it to be). |
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
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
The text was updated successfully, but these errors were encountered: