简体   繁体   中英

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.

I get the following msg: The 'Accept' header must be modified using the appropriate property or method. 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.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. Then you could simply use: httpWebRequestusr.Accept = "application/json";

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