(1)获取字符串数据
HttpClient类使用基于任务的异步模式提供了非常简化的请求操作,你可以直接调用HttpClient类GetStringAsync方法便可以获取到网络返回的字符串数据。下面我们来看一下使用Get请求来获取网络返回的字符串,实现的代码很简洁和简单,示例代码如下所示:
1 2 3 4 | HttpClient httpClient = new HttpClient(); // 获取网络的返回的字符串数据 string result = await httpClient.GetStringAsync (uri); |
使用GetStringAsync方法是一种简化的HTTP请求,如果我们要获取到HTTP请求所返回的整个对象HttpResponseMessage类对象可以使用GetAsync方法。HttpResponseMessage对象是HTTP的相应消息对象,它包含了网络请求相应的HTTP头、数据体等信息。下面我们使用GetAsync方法来获取网络返回的字符串信息,示例代码如下所示:
1 2 | HttpResponseMessage response = await httpClient.GetAsync(uri); string responseBody = await response.Content.ReadAsStringAsync(); |
(2)获取数据流数据
HttpResponseMessage对象的Content属性表示是返回的数据对象,是一个IHttpContent类型的对象。如果要获取的是数据流数据,可以通过它的ReadAsBufferAsync方法获取到返回的IBuffer对象,或者通过ReadAsInputStreamAsync地方获取IInputStream对象,然后再转化为Stream对象,示例代码如下所示:
1 2 3 4 5 6 7 8 9 10 | using (Stream responseStream = (await response.Content.ReadAsInputStreamAsync()).AsStreamForRead()) { int read = 0; byte [] responseBytes = new byte [1000]; do { // 如果read等于0表示Stream的数据以及读取完毕 read = await responseStream.ReadAsync(responseBytes, 0, responseBytes.Length); } while (read != 0); } |
(3)取消网络请求
HttpClient类发起的网络请求都是基于任务的异步方法,所以要取消其异步的操作可以通过异步任务的取消对象CancellationTokenSource对象来取消,这点和HttpWebRequest类是不同。如果使用CancellationTokenSource对象来取消异步的请求会触发TaskCanceledException异常,这个异常需要我们用try catch语句来捕获,便可以识别到请求是被取消的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | private CancellationTokenSource cts = new CancellationTokenSource(); try { // 使用CancellationTokenSource对象来控制异步任务的取消操作 HttpResponseMessage response = await httpClient.GetAsync( new Uri(resourceAddress)).AsTask(cts.Token); responseBody = await response.Content.ReadAsStringAsync().AsTask(cts.Token); cts.Token.ThrowIfCancellationRequested(); } catch (TaskCanceledException) { responseBody = "请求被取消" ; } // 调用Cancel方法取消网络请求 if (cts.Token.CanBeCanceled) { cts.Cancel(); } |
(1)Post请求发送字符串数据
1 2 3 4 | HttpStringContent httpStringContent = new HttpStringContent( "hello Windows Phone" ); HttpResponseMessage response = await httpClient.PostAsync(uri, httpStringContent).AsTask(cts.Token); string responseBody = await response.Content.ReadAsStringAsync().AsTask(cts.Token); |
(2)Post请求发送数据流数据
1 2 3 4 | HttpStreamContent streamContent = new HttpStreamContent(stream.AsInputStream()); HttpResponseMessage response = await httpClient.PostAsync(uri, streamContent).AsTask(cts.Token); string responseBody = await response.Content.ReadAsStringAsync().AsTask(cts.Token); |
除了使用PostAsync方法之外,我们还可以使用SendRequestAsync方法来发送网络请求,SendRequestAsync方法既可以使用Get方式也可以使用Post方式。SendRequestAsync方法发送的消息类型是HttpRequestMessage类对象,HttpRequestMessage类表示HTTP的请求消息类,你可以通过HttpRequestMessage对象设置请求的类型(Get/Post)和传输的数据对象。使用SendRequestAsync方法的代码示例如下所示:
1 2 3 4 5 6 7 | // 创建HttpRequestMessage对象 HttpStreamContent streamContent = new HttpStreamContent(stream.AsInputStream()); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(resourceAddress)); request.Content = streamContent; // 发送数据 HttpResponseMessage response = await httpClient.SendRequestAsync(request).AsTask(cts.Token); string responseBody = await response.Content.ReadAsStringAsync().AsTask(cts.Token); |
1 2 3 4 5 6 7 8 | // 创建一个HttpBaseProtocolFilter对象 HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter(); // 通过HttpBaseProtocolFilter对象获取使用HttpClient进行过网络请求的地址的Cookie信息 HttpCookieCollection cookieCollection = filter.CookieManager.GetCookies( new Uri(resourceAddress)); // 遍历整个Cookie集合的Cookie信息 foreach (HttpCookie cookie in cookieCollection) { } |
当然我们在发送HTTP请求的时候也一样可以带上Cookie信息,如果服务器可以识别到Cookie信息那么就会通过Cookie信息来进行一些操作,比如Cookie信息信息带有用户名和密码的加密信息,那么就可以免去登陆的步骤。在HttpClient的网路请求里面HttpCookie类表示是一个Cookie对象,创建好Cookie对象之后通过HttpBaseProtocolFilter对象的CookieManager属性来设置Cookie,然后发送网络请求,这时候的网络请求就会把Cookie信息给带上。设置Cookie的代码示例如下所示:
1 2 3 4 5 6 7 8 9 | // 创建一个HttpCookie对象,"id"表示是Cookie的名称,"localhost"是主机名,"/"是表示服务器的虚拟路径 HttpCookie cookie = new HttpCookie( "id" , "yourwebsite.com" , "/" ); // 设置Cookie的值 cookie.Value = "123456" ; // 设置Cookie存活的时间,如果设置为null表示只是在一个会话里面生效 cookie.Expires = new DateTimeOffset(DateTime.Now, new TimeSpan(0, 1, 8)); // 在过滤器里面设置Cookie HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter(); bool replaced = filter.CookieManager.SetCookie(cookie, false ); |
HttpClient的网络请求是支持进度监控,通过异步任务的IProgress<T>对象可以直接监控到HttpClient的网络请求返回的进度信息,返回的进度对象是HttpProgress类对象。在进度对象HttpProgress里面包含了下面的一些信息:Stage(当前的状态)、BytesSent(已发送的数据大小)、BytesReceived(已接收的数据大小)、Retries(重试的次数)、TotalBytesToSend(总共需要发送的数据大小)和TotalBytesToReceive(总共需要接收的数据大小)。网络请求进度监控的代码示例如下所示:
1 2 3 4 5 6 7 8 9 | // 创建IProgress<HttpProgress>对象 IProgress<HttpProgress> progress = new Progress<HttpProgress>(ProgressHandler); // 在异步任务中加入进度监控 HttpResponseMessage response = await httpClient.PostAsync( new Uri(resourceAddress), streamContent).AsTask(cts.Token, progress); // 进度监控的回调方法 private void ProgressHandler(HttpProgress progress) { // 在这里可以通过progress参数获取到进度的相关信息 } |
原文:http://www.cnblogs.com/mjxup/p/6420542.html