是否有 C# 或 .NET 类来处理与用户代理的 HTTP 内容协商?
我希望能够提供可接受的内容类型列表,并与浏览器协商以找到最佳匹配。
我在这里写了博客。
我认为用户代理这个词在您的问题中有点偏离,但是如果您想构建请求某个来源(可以说是一个 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();
}