1

Should I still add my headers to HttpRequestMessageif I already have add on my httpclient headers? what is the difference of adding from httpclient first and then requestMessage?

like this:

 public static async Task<JsonDocument> ParseJsonData(string api, string method, CancellationToken cancellationToken)
    {
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
        client.DefaultRequestHeaders.Add("Accept", "text/html,application/xml,application/json");
        client.DefaultRequestHeaders.Add("Accept-Language", "en-us,en;q=0.5");
        client.DefaultRequestHeaders.UserAgent.ParseAdd(UserAgent);

        var uri = new Uri(api, UriKind.Absolute);

        var Method = new HttpMethod(method);

        using (var request = new HttpRequestMessage(Method, uri))
        {
            cancellationToken.ThrowIfCancellationRequested();

            request.RequestUri = uri;
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            request.Headers.Pragma.ParseAdd("no-cache");
            request.Headers.Add("Cache-Control", "no-cache");
            request.Headers.Connection.ParseAdd("keep-alive");
            request.Headers.UserAgent.ParseAdd(UserAgent);
        
         switch (Method.ToString().ToUpper())
            {
                case "POST":

                    string loginInfo = "{\"login_id\": \"" + credential.username + 
                                    "\", \"password\": \"" + credential.password + "\"}";
                    using (var content = new StringContent(loginInfo, Encoding.UTF8, "text/plain"))
                    {
                        request.Content = content;
                        return await ResponseMessage(request, cancellationToken);
                    }

                case "GET":
                    {
                        return await ResponseMessage(request, cancellationToken);
                    }
            }

1 Answer 1

2

Both collections are merged when sending a request. If there's a duplicate header, both will be sent out.

Sign up to request clarification or add additional context in comments.

4 Comments

So, it's like the same if I put the header only either to httpclientdefaultrequest or httpreqeuestmessage?
where is the best to input my headers? because I'm very confused if I still need to put all of them
When you are making multiple requests, and you have some headers that need to go out for every request, use default headers. When you are making a single request, or have headers that don't need to go out for every request, use per-message headers.
thank you for the information. so If I input the headers to defaultrequestheaders, I don't need to input under request header?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.