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

时间:2018-10-13 15:30:28

标签: c# http http-headers httpclient

使用

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

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

1 个答案:

答案 0 :(得分:0)

FormUrlEncodedContent类继承自HttpContent,其中包含Headers属性,您可以使用该属性添加/删除/设置http标头。

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

示例:

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