HttpClient 标头与 HttpContent 标头
在 C# 中,为什么 HttpClient 和 HttpContent 都有标头。他们之间有什么区别。我什么时候应该使用客户端标头,什么时候应该使用内容标头?
代码示例:
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Test header", "content");
HttpContent content = new StringContent("text", Encoding.UTF8, "application/json");
content.Headers.Add("TestHeader", "Header Content");
await client.PostAsync("url", content);
In c# why both HttpClient and HttpContent have headers. What is difference between them. When should I use client headers and when content headers ?
CODE EXAMPLE:
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Test header", "content");
HttpContent content = new StringContent("text", Encoding.UTF8, "application/json");
content.Headers.Add("TestHeader", "Header Content");
await client.PostAsync("url", content);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HttpClient 支持多种类型的内容。例如:
有关受支持内容的完整列表,请参阅 < a href="https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpcontent?view=net-6.0" rel="nofollow noreferrer">HttpContent。
HttpContent 包含一些有关内容的更具体的标头,包括内容类型。
我认为这个列表 这里可以让您很好地了解可用的标头。我确实同意,只有一组标头会让事情变得更容易。
HttpClient supports several types of content. For example:
For a complete list of supported content, see HttpContent.
HttpContent contains some more specific headers about the content, including the content type.
I think this list here can give you a pretty good understanding on what headers that are available. I do agree that having jus one set of headers would make things much easier.