-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Allow instantiate HttpRequestHeaders and merge it into another instance #21209
Comments
Can you provide a link to the source code you are talking about? |
If you want to set additional headers per request and based on a custom set of rules, it might be better to use a customer HttpMessageHandler. You can create a new custom handler based on a DelegatingHandler class: Then you can use this handler when you create an HttpClient(), i.e. var handler = new CustomHandler();
var client = new HttpClient(handler); Using a custom handler such as this is the recommended best-practice way of adding custom behavior like this (i.e. adding request headers based on custom rules). |
cc: @karelz @stephentoub |
@davidsh, I want to write something like this: class Program
{
static void Main(string[] args)
{
var client = new MyHttpClient();
client.DefaultRequestHeaders.Add("X-SHARED", "XYZ");
HttpRequestHeaders googleDefaultHeaders = new HttpRequestHeaders
{
{ "X-MY-HEADER-1", "123" },
{ "X-MY-HEADER-2", "456" },
};
client.PerHostDefaultHeaders.Add(new Uri("HTTP://www.google.com"), googleDefaultHeaders);
HttpRequestHeaders twitterDefaultHeaders = new HttpRequestHeaders
{
{ "X-MY-HEADER-A", "BBBB" },
{ "X-MY-HEADER-B", "BABABA" },
};
client.PerHostDefaultHeaders.Add(new Uri("HTTP://www.twitter.com"), twitterDefaultHeaders);
// Do something
}
}
public class MyHttpClient : HttpClient
{
public Dictionary<Uri, HttpRequestHeaders> PerHostDefaultHeaders { get; } = new Dictionary<Uri, HttpRequestHeaders>();
public override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var key = PerHostDefaultHeaders.Keys.FirstOrDefault(x => x.IsBaseOf(request.RequestUri));
if (key != null)
{
request.Headers.AddHeaders(PerHostDefaultHeaders[key]);
}
return base.SendAsync(request, cancellationToken);
}
} This code have 2 problems:
|
@karelz, I know public class MyHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
switch(request.RequestUri.Host.ToLower())
{
case "www.google.com":
if(!request.Headers.ConnectionClose.HasValue)
{
request.Headers.ConnectionClose = true;
}
if(!request.Headers.Contains("X-My-Header"))
{
request.Headers.Add("X-My-Header", "XXX");
}
break;
case "xxx.xxx.com":
// Set general headers
// set custom headers
break;
}
return base.SendAsync(request, cancellationToken);
}
}
System.Net.Http.HttpClient does it with a single line of code: |
Did you consider adding your If this is common task in real-world apps (so far we got only 1 report), we could consider adding it into In general we want to add APIs which are widely used, important. If we add API for every single thing anyone will ask, we will create monster classes which people will have trouble to use, because it will be too hard to figure out what it is they need to do / have to do, etc. Again, for common tasks it definitely makes sense to add APIs. Makes sense? |
@karelz, I have two problems:
It is not a new feature. It exists already, and it is used internally. All I want is to make it public. |
Sadly, there is no such thing as "just make the API public" :(. Public APIs need extra care - you have to think through all angles, all usage cases. You have to understand how it impacts your ability to evolve the API in future. Key questions: Who is going to benefit form the API? How common the API is? Are we going to regret the API in 3-5 years, because there will be something better/shinier? If you are still interested, please read our API process and feel free to submit the proposal with all/most questions above answered, demonstrating need for the API in the ecosystem/API surface and demonstrating understanding of all its impact in all dimensions. If there is compelling case, we will definitely consider it. |
@karelz, What more information do you expect from me? |
@YehudahA your description talks about implementation details. It doesn't talk about scenarios, usage patterns and other key questions I raised above. Tip & tricks:
|
dotnet/corefx#23544 seems to be reasonable way how to solve the problem. |
System.Net.Http.HttpRequestHeaders
is a comfortable class, and it stores all general headers [UserAgent, Connection, Encoding etc.] and custom headers.This class used by
HttpClient
asDefaultRequestHeaders
property, for merge default headers into everyHttpRequestMessage
.Example for internal using of this for set default headers: HttpClient.PrepareRequestMessage
The internal logic that merge headers: HttpRequestHeaders.AddHeaders
Unfortunately, This class have 2 limitations:
HttpRequestHeaders
into another instance ofHttpRequestHeaders
, is not public.I want to extend
HttpClient
orHttpMessageHandler
behavior, so I can create per-hostDefaultRequestHeaders
. Something like this:[Sorry for my bad English ):]
The text was updated successfully, but these errors were encountered: