Jamie
Jamie

Reputation: 71

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

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.

Upvotes: 6

Views: 722

Answers (2)

Mauricio Scheffer
Mauricio Scheffer

Reputation: 99750

I recently wrote a content negotiation library in F#.

I blogged about it here.

Upvotes: 1

Didier Caron
Didier Caron

Reputation: 570

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

Upvotes: 0

Related Questions