简体   繁体   English

在Xamarin.Forms c#中设置HttpRequestHeader.Accept

[英]Set HttpRequestHeader.Accept in Xamarin.Forms c#

I have tried this, but its not working: 我已经尝试过了,但是没有用:

httpWebRequestusr.Headers[HttpRequestHeader.Accept] = "application/vnd.api+json";

It is works fine in native uwp app, but not in xamarin.forms. 它在本机uwp应用程序中运行良好,但在xamarin.forms中却无法运行。

I get the following msg: The 'Accept' header must be modified using the appropriate property or method. 我得到以下消息:必须使用适当的属性或方法修改'Accept'标头。 Parameter name: name 参数名称:名称

What is the best way to set "Accept"? 设置“接受”的最佳方法是什么?

You can either set it on the HttpClient if you want it to be for all requests: 如果希望所有请求都可以在HttpClient上进行设置:

HttpClient.DefaultRequestHeaders.Add("Accept", "application/json");

Or just on the one request: 或仅根据一个请求:

httpWebRequestusr.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

As we gathered from our little chat below this answer, you are using the HttpWebRequest object. 当我们从这个答案下面的HttpWebRequest聊天中收集信息时,您正在使用HttpWebRequest对象。 Then you could simply use: httpWebRequestusr.Accept = "application/json"; 然后,您可以简单地使用: httpWebRequestusr.Accept = "application/json";

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

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