设为首页 加入收藏

TOP

揭秘Windows10 UWP中的httpclient接口[2](二)
2017-10-11 16:06:56 】 浏览:5424
Tags:揭秘 Windows10 UWP httpclient 接口
n Custom Handler 1
"); HttpResponseMessage response = await base.SendAsync(request, cancellationToken); Debug.WriteLine("Processing response in Custom Handler 1"); return response; } }
public class CustomHandler2 : DelegatingHandler
{
    // Similar code as CustomHandler1.
}
public class Foo
{
    public void CreateHttpClientWithChain()
    {
        HttpClientHandler systemHandler = new HttpClientHandler();
        CustomHandler1 myHandler1 = new CustomHandler1();
        CustomHandler2 myHandler2 = new CustomHandler2();
 
        // Chain the handlers together.
        myHandler1.InnerHandler = myHandler2;
        myHandler2.InnerHandler = systemHandler;
 
        // Create the client object with the topmost handler in the chain.
        HttpClient myClient = new HttpClient(myHandler1);
    }
}

说明:

如果你试图发送一个请求到远程服务器端口上,其链上最后的处理器通常是HttpClientHandler,它实际是从系统HTTP协议栈层面发送这个请求或接收这个响应。作为一种选择,可以使用一个模拟处理器,模拟发送请求到服务器上,返回一个伪造的响应,这可以用来单元测试。

在传递请求到内部处理器之前或响应处理器之上,添加一个处理逻辑,能减少性能消耗。这个处理器场景下,最好能避免使用耗时的同步操作。

关于链式处理概念的详细信息,可以看Henrik Nielsen的这篇博客,(注意文章参考的是ASP.NET Web API的API版本。它和本文讨论的.NET framework有一些细微的不同,但在链式处理器上的概念是一样的)

Windows.Web.Http

Windows.Web.Http API的对象模型跟上面描述的System.Net.Http版本非常 ,它也有client entity的概念,一个处理器(在这叫“filter”过滤器),及在client和系统默认过滤器之间选择是否插入自定义逻辑。

其大多数类型是直接类似于System.Net.Http的类型的,如下:

在上面关于System.Net.Http API的链式处理器讨论,也可应用于Windows.Web.Http API,这里你可以创建自定义链式过滤器,传递它们到HttpClient对象的构造函数中。

HTTP的常用功能

关于HttpClient APIs中的大多数HTTP功能的通用实现,都能在网上或书上找到一些代码片段和相应介绍说明。关于完整的细节和指导,请查看Windows.Web.Http.HttpClient和System.Net.Http.HttpClient API各自的MSDN文档。

修改头部

System.Net.Http:

在HttpClient实例上修改所有请求的头部,使用下面的方式:

var myClient = new HttpClient(); 
myClient.DefaultRequestHeaders.Add("X-HeaderKey", "HeaderValue");
myClient.DefaultRequestHeaders.Referrer = new Uri("http://www.contoso.com");

 只修改指定请求的头部,使用:

HttpRequestMessage myrequest = new HttpRequestMessage(); 
myrequest.Headers.Add("X-HeaderKey", "HeaderValue");
myrequest.Headers.Referrer = new Uri("http://www.contoso.com");

 Windows.Web.Http:

上面的方式同样适用于Windows.Web.Http API。

说明

一些头部是用集合表示的,要使添加和移除方法去编辑它们。

HttpClient.DefaultRequestHeaders属性表示默认头部集合,它会在App层添加到头部。请求会在操作系统协议栈上被处理,附加的头部会在数据通过网卡发送之前被添加。

设置超时

System.Net.Http:

在the System.Net.Http API中,有两个方式去设置超时。 在client部分上设置所有请求的超时时间,使用:

myClient.Timeout = TimeSpan.FromSeconds(30);

 在单个请求上设置超时,使用删除token方式:

var cts = new CancellationTokenSource(); 
cts.CancelAfter(TimeSpan.FromSeconds(30));
var httpClient = new HttpClient();
var resourceUri = new Uri("http://www.contoso.com"); try {
HttpResponseMessage response = await httpClient.GetAsync(resourceUri, cts.Token);
} catch (TaskCanceledException ex)
{ // Handle request being canceled due to timeout. } catch (HttpRequestException ex) { // Handle other possible exceptions. }

 Windows.Web.Http:

在Windows.Web.Http.HttpClient上没有超时属性,因此,必须使用上面介绍的删除token方式实现超时功能。

使用身份验证凭据

System.Net.Http:

为了保护用户凭据信息,默认情况下Http协议栈在请求发出时,不能添加任务身份验证信息。如需要使用指定用户验证,使用下面的模式:

var myClientHandler = new HttpClientHandler(); 
myClientHandler.Credentials = new NetworkCredential(myUsername, myPassword);

Windows.Web.Http:

对于Windows.Web.Http API,默认情况下,如果发出的请求是一

首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇07.移动先行之谁主沉浮----控件之.. 下一篇Win10 FaceAPI小demo开发问题汇总

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目