开发者

Is there a C# or .NET class to handle HTTP Content Negotiation?

开发者 https://www.devze.com 2022-12-31 21:25 出处:网络
Is there a C# or .NET class to handle HTTP Content Negotiation with a User Agent? I would like to be able to supply a list of acceptable Content Types, and have those negotiated with the browser to f

Is there a C# or .NET class to handle HTTP Content Negotiation with a User Agent?

I would like to be able to supply a list of acceptable Content Types, and have those negotiated with the browser to find the best match开发者_运维技巧.


I recently wrote a content negotiation library in F#.

I blogged about it here.


I think the word user agent is a bit off in your question but if you want to build request a certain source (lets say a restfull api). You can use the WCF Rest Starter kit (http://wcf.codeplex.com/) to specify the type of content you want or accept:

HttpClient client = new HttpClient(new Uri("http://restfull/api/"));
//this is XML but could be JSON or whatever the API can supply
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
var response = client.Get(path);

if (response != null)
{
  response.EnsureSuccessStatusCode();
  //this will be XML
  string xml = response.Content.ReadAsString();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消