简体   繁体   中英

How do I add header data in HttpClient/PostAsync

using

            var values = new Dictionary<string, string>
            {
               { "thing1", "hello" },
               { "thing2", "world" }
            };
            var content = new FormUrlEncodedContent(values);
            var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);

            var responseString = await response.Content.ReadAsStringAsync();

I have not seen an examnle with adding headers, only data values

FormUrlEncodedContent class inherits from HttpContent , which contains the Headers property, which you can use to add/remove/set the http headers.

The Headers property is an instance of HttpContentHeaders , so check that last class' docs to see the available methods and properties you can use to alter the headers you want.

Example:

var content = new FormUrlEncodedContent(values);
content.Headers.Add("MyHeader", "My Value");
content.Headers.ContentType = "application/pdf";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM