6

是否有 C# 或 .NET 类来处理与用户代理的 HTTP 内容协商?

我希望能够提供可接受的内容类型列表,并与浏览器协商以找到最佳匹配。

4

2 回答 2

1

我最近在 F# 中编写了一个内容协商库

我在这里写了博客。

于 2011-06-10T04:43:35.680 回答
0

我认为用户代理这个词在​​您的问题中有点偏离,但是如果您想构建请求某个来源(可以说是一个 restfull api)。您可以使用 WCF Rest Starter 工具包 (http://wcf.codeplex.com/) 来指定您想要或接受的内容类型:

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();
}
于 2011-02-28T21:52:09.637 回答