繁体   English   中英

如何在HttpClient / PostAsync中添加标头数据

[英]How do I add header data in HttpClient/PostAsync

运用

            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();

我还没有看到添加标头的例子,只有数据值

FormUrlEncodedContent类继承自HttpContent ,后者包含Headers属性,可用于添加/删除/设置http标头。

Headers属性是HttpContentHeaders的一个实例,因此请检查上一类的文档,以查看可用于更改所需标题的可用方法和属性。

例:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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