Jam*_*mie 6 c# http content-negotiation
是否有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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
677 次 |
| 最近记录: |