Custom header to HttpClient request

前端 未结 4 1714
走了就别回头了
走了就别回头了 2020-11-27 03:38

How do I add a custom header to a HttpClient request? I am using PostAsJsonAsync method to post the JSON. The custom header that I would need to b

相关标签:
4条回答
  • 2020-11-27 04:07

    Here is an answer based on that by Anubis (which is a better approach as it doesn't modify the headers for every request) but which is more equivalent to the code in the original question:

    using Newtonsoft.Json;
    ...
    
        var client = new HttpClient();
        var httpRequestMessage = new HttpRequestMessage
            {
                Method = HttpMethod.Post,
                RequestUri = new Uri("https://api.clickatell.com/rest/message"),
                Headers = { 
                    { HttpRequestHeader.Authorization.ToString(), "Bearer xxxxxxxxxxxxxxxxxxxx" },
                    { HttpRequestHeader.Accept.ToString(), "application/json" },
                    { "X-Version", "1" }
                },
                Content = new StringContent(JsonConvert.SerializeObject(svm))
            };
    
        var response = client.SendAsync(httpRequestMessage).Result;
    
    0 讨论(0)
  • 2020-11-27 04:18
    var request = new HttpRequestMessage {
        RequestUri = new Uri("[your request url string]"),
        Method = HttpMethod.Post,
        Headers = {
            { "X-Version", "1" } // HERE IS HOW TO ADD HEADERS,
            { HttpRequestHeader.Authorization.ToString(), "[your authorization token]" },
            { HttpRequestHeader.ContentType.ToString(), "multipart/mixed" },//use this content type if you want to send more than one content type
        },
        Content = new MultipartContent { // Just example of request sending multipart request
            new ObjectContent<[YOUR JSON OBJECT TYPE]>(
                new [YOUR JSON OBJECT TYPE INSTANCE](...){...}, 
                new JsonMediaTypeFormatter(), 
                "application/json"), // this will add 'Content-Type' header for the first part of request
            new ByteArrayContent([BINARY DATA]) {
                Headers = { // this will add headers for the second part of request
                    { "Content-Type", "application/Executable" },
                    { "Content-Disposition", "form-data; filename=\"test.pdf\"" },
                },
            },
        },
    };
    
    0 讨论(0)
  • 2020-11-27 04:29

    There is a Headers property in the HttpRequestMessage class. You can add custom headers there, which will be sent with each HTTP request. The DefaultRequestHeaders in the HttpClient class, on the other hand, sets headers to be sent with each request sent using that client object, hence the name Default Request Headers.

    Hope this makes things more clear, at least for someone seeing this answer in future.

    0 讨论(0)
  • 2020-11-27 04:32

    I have found the answer to my question.

    client.DefaultRequestHeaders.Add("X-Version","1");
    

    That should add a custom header to your request

    0 讨论(0)
提交回复
热议问题