• <bdo id='CzFfA'></bdo><ul id='CzFfA'></ul>

      <legend id='CzFfA'><style id='CzFfA'><dir id='CzFfA'><q id='CzFfA'></q></dir></style></legend>

      1. <i id='CzFfA'><tr id='CzFfA'><dt id='CzFfA'><q id='CzFfA'><span id='CzFfA'><b id='CzFfA'><form id='CzFfA'><ins id='CzFfA'></ins><ul id='CzFfA'></ul><sub id='CzFfA'></sub></form><legend id='CzFfA'></legend><bdo id='CzFfA'><pre id='CzFfA'><center id='CzFfA'></center></pre></bdo></b><th id='CzFfA'></th></span></q></dt></tr></i><div id='CzFfA'><tfoot id='CzFfA'></tfoot><dl id='CzFfA'><fieldset id='CzFfA'></fieldset></dl></div>

      2. <small id='CzFfA'></small><noframes id='CzFfA'>

      3. <tfoot id='CzFfA'></tfoot>

        HttpClient 请求的自定义标头

        Custom header to HttpClient request(HttpClient 请求的自定义标头)
          <tbody id='sR663'></tbody>

        <small id='sR663'></small><noframes id='sR663'>

              <bdo id='sR663'></bdo><ul id='sR663'></ul>
              <i id='sR663'><tr id='sR663'><dt id='sR663'><q id='sR663'><span id='sR663'><b id='sR663'><form id='sR663'><ins id='sR663'></ins><ul id='sR663'></ul><sub id='sR663'></sub></form><legend id='sR663'></legend><bdo id='sR663'><pre id='sR663'><center id='sR663'></center></pre></bdo></b><th id='sR663'></th></span></q></dt></tr></i><div id='sR663'><tfoot id='sR663'></tfoot><dl id='sR663'><fieldset id='sR663'></fieldset></dl></div>

              • <tfoot id='sR663'></tfoot>
                <legend id='sR663'><style id='sR663'><dir id='sR663'><q id='sR663'></q></dir></style></legend>
                • 本文介绍了HttpClient 请求的自定义标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何向 HttpClient 请求添加自定义标头?我正在使用 PostAsJsonAsync 方法发布 JSON.我需要添加的自定义标题是

                  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 be added is

                  "X-Version: 1"
                  

                  这是我到目前为止所做的:

                  This is what I have done so far:

                  using (var client = new HttpClient()) {
                      client.BaseAddress = new Uri("https://api.clickatell.com/");
                      client.DefaultRequestHeaders.Accept.Clear();
                      client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "xxxxxxxxxxxxxxxxxxxx");
                      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                      var response = client.PostAsJsonAsync("rest/message", svm).Result;
                  }
                  

                  推荐答案

                  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"" },
                              },
                          },
                      },
                  };
                  

                  这篇关于HttpClient 请求的自定义标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding and removing users from Active Directory groups in .NET(在 .NET 中的 Active Directory 组中添加和删除用户)
                  set equality in linq(在 linq 中设置相等)
                  HashSet conversion to List(HashSet 转换为 List)
                  How to set timeout for webBrowser navigate event(如何为 webBrowser 导航事件设置超时)
                  Test whether two IEnumerablelt;Tgt; have the same values with the same frequencies(测试两个IEnumerablelt;Tgt;具有相同频率的相同值)
                  How do you determine if two HashSets are equal (by value, not by reference)?(您如何确定两个 HashSet 是否相等(按值,而不是按引用)?)
                    <bdo id='CbEHS'></bdo><ul id='CbEHS'></ul>
                      <legend id='CbEHS'><style id='CbEHS'><dir id='CbEHS'><q id='CbEHS'></q></dir></style></legend>
                    1. <i id='CbEHS'><tr id='CbEHS'><dt id='CbEHS'><q id='CbEHS'><span id='CbEHS'><b id='CbEHS'><form id='CbEHS'><ins id='CbEHS'></ins><ul id='CbEHS'></ul><sub id='CbEHS'></sub></form><legend id='CbEHS'></legend><bdo id='CbEHS'><pre id='CbEHS'><center id='CbEHS'></center></pre></bdo></b><th id='CbEHS'></th></span></q></dt></tr></i><div id='CbEHS'><tfoot id='CbEHS'></tfoot><dl id='CbEHS'><fieldset id='CbEHS'></fieldset></dl></div>
                          <tbody id='CbEHS'></tbody>
                        <tfoot id='CbEHS'></tfoot>

                        <small id='CbEHS'></small><noframes id='CbEHS'>