Configure Accept Header without using MediaTypeWithQualityHeaderValue

In Asp.Net Web Api 2, what is the difference between setting the HttpClient Accept Header header using the following traditional method:

        HttpClient client = HttpClientFactory.Create(handler);

        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

and the following method:

var headers = new Dictionary<string, string>
            {
                {"Accept", "application/json"}};

headers.ForEach(h => client.DefaultRequestHeaders.Add(h.Key, h.Value));

Update 1:

Based on @ DarrenMiller's answer in the following post What are the overhead of creating a new HttpClient for each call in the WebAPI client? , it seems that the preferred method uses the property DefaultRequestHeadersbecause it contains properties intended for multiple calls. Does this mean if I set the default title using a simple dictionary, mine HttpClient clientwill not be as effective as the one that uses DefaultRequestHeaders? Also, I really can't figure out how the values ​​inside DefaultRequestHeaderswill be reused? Suppose I created 20 HttpClient clientusing HttpClientFactory.Create, and inside each of them I set the propertyDefaultRequestHeaders[I really need to do this because DefaultRequestHeaders was intended to be reused ?!]. Where is this reuse of kick-in and does it set a value DefaultRequestHeadersevery time I create a result HttpClient clientas a result of some kind of performance improvement?

+4
source share
1 answer

The first part of your question: is there a difference for adding headers?

HttpClient client = HttpClientFactory.Create(handler);

Method 1:

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

Method 2:

var headers = new Dictionary<string, string>{{"Accept", "application/json"}};
headers.ForEach(h => client.DefaultRequestHeaders.Add(h.Key, h.Value));

Method 1 gives you good strongly typed values ​​with the ability to add multiple accept types. In method 2, there is another “magic line” that can be a place for typos, and there is no way to add several types of adoption.

2 : ?

HttpClient . , . , , . , HttpClient , . , . , DefaultRequestHeaders, factory.

public class ApiService
{
    public static HttpClient GetClient()
    {
        var client = new HttpClient(new Uri("https://someservice/"));
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        //add any other setup items here.
        return client;
    }
}

:

public async Task DoStuff()
{
    using(var client = ApiService.GetClient())
    {
        //client will have the proper base uri and all the headers set.
        var data = await client.GetAsync<dynamic>("Sales");

        //client will still have the proper base uri and all the headers set.
        var data2 = await client.GetAsync<dynamic>("Products");
    }
}

HttpClients using. , .

0

All Articles