[源码下载]

重新想象 Windows 8.1 Store Apps (88) - 通信的新特性: 新的 HttpClient

作者:webabcd

介绍
重新想象 Windows 8.1 Store Apps 之通信的新特性

  • 新的 HttpClient
  • http get string
  • http get stream
  • http post string
  • http post stream

示例
HTTP 服务端
WebServer/HttpDemo.aspx.cs

/*
* 用于响应 http 请求
*/ using System;
using System.IO;
using System.Threading;
using System.Web; namespace WebServer
{
public partial class HttpDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 停 3 秒,以方便测试 http 请求的取消
Thread.Sleep(); var action = Request.QueryString["action"]; switch (action)
{
case "getString": // 响应 http get string
Response.Write("hello webabcd: " + DateTime.Now.ToString("hh:mm:ss"));
break;
case "getStream": // 响应 http get stream
Response.Write("hello webabcd hello webabcd hello webabcd hello webabcd hello webabcd hello webabcd hello webabcd hello webabcd hello webabcd hello webabcd hello webabcd hello webabcd");
break;
case "postString": // 响应 http post string
Response.Write(string.Format("param1:{0}, param2:{1}, referrer:{2}", Request.Form["param1"], Request.Form["param2"], Request.UrlReferrer));
break;
case "postStream": // 响应 http post stream
using (StreamReader reader = new StreamReader(Request.InputStream))
{
if (Request.InputStream.Length > * )
{
// 接收的数据太大,则显示“数据接收成功”
Response.Write("数据接收成功");
}
else
{
// 显示接收到的数据
string body = reader.ReadToEnd();
Response.Write(Server.HtmlEncode(body));
}
}
break;
case "uploadFile": // 处理上传文件的请求
for (int i = ; i < Request.Files.Count; i++)
{
string key = Request.Files.GetKey(i);
HttpPostedFile file = Request.Files.Get(key);
string savePath = @"d:\" + file.FileName; // 保存文件
file.SaveAs(savePath); Response.Write(string.Format("key: {0}, fileName: {1}, savePath: {2}", key, file.FileName, savePath));
Response.Write("\n");
}
break;
case "outputCookie": // 用于显示服务端获取到的 cookie 信息
for (int i = ; i < Request.Cookies.Count; i++)
{
HttpCookie cookie = Request.Cookies[];
Response.Write(string.Format("cookieName: {0}, cookieValue: {1}", cookie.Name, cookie.Value));
Response.Write("\n");
}
break;
case "outputCustomHeader": // 用于显示一个自定义的 http header
Response.Write("myRequestHeader: " + Request.Headers["myRequestHeader"]);
break;
default:
break;
} Response.End();
}
}
}

1、演示新的 HttpClient
Summary.xaml.cs

/*
* 本例简要说明新的命名空间 Windows.Web.Http 下的新的 HttpClient 的用法(其他相关类也均来自新的 Windows.Web.Http 命名空间)
* 通过 HttpClient, HttpRequestMessage, HttpResponseMessage 实现 HTTP 通信
*
*
* 在 win8.1 中增强了 HttpClient 的功能并将此增强版的 HttpClient 转移至了 Windows.Web.Http 命名空间下(原 win8 的 HttpClient 在 System.Net.Http 命名空间下,依旧可用)
* 1、如果要查看 System.Net.Http 命名空间下的 HttpClient 的用法请参见:http://www.cnblogs.com/webabcd/archive/2013/09/23/3334233.html
* 2、关于支持 OAuth 2.0 验证的客户端也请参见:http://www.cnblogs.com/webabcd/archive/2013/09/23/3334233.html
*
*
* HttpClient - 用于发起 http 请求,以及接收 http 响应
* GetStringAsync(), GetAsync(), GetBufferAsync(), GetInputStreamAsync() - http get 请求
* DeleteAsync() - http delete 请求
* PostAsync(), PutAsync() - http post/put 请求
* 参数:IHttpContent content - http 请求的数据
* 实现了 IHttpContent 的类有:HttpStringContent, HttpBufferContent, HttpStreamContent, HttpFormUrlEncodedContent, HttpMultipartFormDataContent 等
* 参数:HttpCompletionOption completionOption(HttpCompletionOption 枚举)
* ResponseContentRead - 获取到全部内容后再返回数据,默认值
* ResponseHeadersRead - 获取到头信息后就返回数据,用于流式获取
* SendRequestAsync() - 根据指定的 HttpRequestMessage 对象发起请求
*
* HttpRequestMessage - http 请求
* Method - http 方法
* RequestUri - 请求的 uri
* Headers - http 的请求头信息
* Properties - 当做上下文用
* Content - http 请求的内容(IHttpContent 类型)
* 实现了 IHttpContent 的类有:HttpStringContent, HttpBufferContent, HttpStreamContent, HttpFormUrlEncodedContent, HttpMultipartFormDataContent 等
* TransportInformation - 获取一个 HttpTransportInformation 类型的对象,用于 https 相关
*
* HttpResponseMessage - http 响应
* RequestMessage - 对应的 HttpRequestMessage 对象
* Headers - http 的响应头信息
* Version - http 版本,默认是 1.1
* Source - 数据来源(HttpResponseMessageSource 枚举:Cache 或 Network)
* StatusCode - http 响应的状态码
* ReasonPhrase - http 响应的状态码所对应的短语
* IsSuccessStatusCode - http 响应的状态码是否是成功的值(200-299)
* EnsureSuccessStatusCode() - 当 IsSuccessStatusCode 为 false 时会抛出异常
*
*
* 另:
* win8 metro 的 http 抓包可用 fiddler
*
* 还有:
* http 通信还可以通过如下方法实现
* HttpWebRequest webRequest = WebRequest.Create(url);
*/ using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using Windows.Web.Http; namespace Windows81.Communication.HTTP
{
public sealed partial class Summary : Page
{
private HttpClient _httpClient;
private CancellationTokenSource _cts; public Summary()
{
this.InitializeComponent();
} protected override void OnNavigatedFrom(NavigationEventArgs e)
{
// 释放资源
if (_httpClient != null)
{
_httpClient.Dispose();
_httpClient = null;
} if (_cts != null)
{
_cts.Dispose();
_cts = null;
}
} private async void btnPost_Click(object sender, RoutedEventArgs e)
{
_httpClient = new HttpClient();
_cts = new CancellationTokenSource(); try
{
string url = "http://localhost:39630/HttpDemo.aspx?action=postString";
// 创建一个 HttpRequestMessage 对象
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(url)); // 需要 post 的数据
HttpFormUrlEncodedContent postData = new HttpFormUrlEncodedContent(
new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("param1", "web"),
new KeyValuePair<string, string>("param2", "abcd")
}
); // http 请求的数据
request.Content = postData;
// http 请求的头信息(叽歪一个,终于把 Referrer 改成 Referer 了)
request.Headers.Referer = new Uri("http://webabcd.cnblogs.com"); // 请求 HttpRequestMessage 对象,并返回 HttpResponseMessage 数据
HttpResponseMessage response = await _httpClient.SendRequestAsync(request).AsTask(_cts.Token); // 取消请求的方式改为通过 CancellationTokenSource 来实现了 // http 响应的状态码及其对应的短语
lblMsg.Text += ((int)response.StatusCode) + " " + response.ReasonPhrase;
lblMsg.Text += Environment.NewLine; // 以字符串的方式获取响应数据
lblMsg.Text += await response.Content.ReadAsStringAsync();
lblMsg.Text += Environment.NewLine;
}
catch (TaskCanceledException)
{
lblMsg.Text += "取消了";
lblMsg.Text += Environment.NewLine;
}
catch (Exception ex)
{
lblMsg.Text += ex.ToString();
lblMsg.Text += Environment.NewLine;
}
} private void btnCancel_Click(object sender, RoutedEventArgs e)
{
// 取消 http 请求
if (_cts != null)
{
_cts.Cancel();
_cts.Dispose();
_cts = null;
}
}
}
}

2、演示 http get string
GetString.xaml.cs

/*
* 演示 http get string
*/ using System;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using Windows.Web.Http; namespace Windows81.Communication.HTTP
{
public sealed partial class GetString : Page
{
private HttpClient _httpClient;
private CancellationTokenSource _cts; public GetString()
{
this.InitializeComponent();
} protected override void OnNavigatedFrom(NavigationEventArgs e)
{
// 释放资源
if (_httpClient != null)
{
_httpClient.Dispose();
_httpClient = null;
} if (_cts != null)
{
_cts.Dispose();
_cts = null;
}
} private async void btnGetString_Click(object sender, RoutedEventArgs e)
{
_httpClient = new HttpClient();
_cts = new CancellationTokenSource(); try
{
HttpResponseMessage response = await _httpClient.GetAsync(new Uri("http://localhost:39630/HttpDemo.aspx?action=getString")).AsTask(_cts.Token); // 取消请求的方式改为通过 CancellationTokenSource 来实现了 lblMsg.Text += ((int)response.StatusCode) + " " + response.ReasonPhrase;
lblMsg.Text += Environment.NewLine; // IHttpContent.ReadAsStringAsync() - 获取 string 类型的响应数据
// IHttpContent.ReadAsBufferAsync() - 获取 IBuffer 类型的响应数据
// IHttpContent.ReadAsInputStreamAsync() - 获取 IInputStream 类型的响应数据
lblMsg.Text += await response.Content.ReadAsStringAsync();
lblMsg.Text += Environment.NewLine;
}
catch (TaskCanceledException)
{
lblMsg.Text += "取消了";
lblMsg.Text += Environment.NewLine;
}
catch (Exception ex)
{
lblMsg.Text += ex.ToString();
lblMsg.Text += Environment.NewLine;
}
} private void btnCancel_Click(object sender, RoutedEventArgs e)
{
// 取消 http 请求
if (_cts != null)
{
_cts.Cancel();
_cts.Dispose();
_cts = null;
}
}
}
}

3、演示 http get stream
GetStream.xaml.cs

/*
* 演示 http get stream
*/ using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Windows.Security.Cryptography;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using Windows.Web.Http; namespace Windows81.Communication.HTTP
{
public sealed partial class GetStream : Page
{
private HttpClient _httpClient;
private CancellationTokenSource _cts; public GetStream()
{
this.InitializeComponent();
} protected override void OnNavigatedFrom(NavigationEventArgs e)
{
// 释放资源
if (_httpClient != null)
{
_httpClient.Dispose();
_httpClient = null;
} if (_cts != null)
{
_cts.Dispose();
_cts = null;
}
} private async void btnGetStream_Click(object sender, RoutedEventArgs e)
{
_httpClient = new HttpClient();
_cts = new CancellationTokenSource(); try
{
// HttpCompletionOption.ResponseHeadersRead - 获取到头信息后就返回数据,用于流式获取
HttpResponseMessage response = await _httpClient.GetAsync(
new Uri("http://localhost:39630/HttpDemo.aspx?action=getStream"),
HttpCompletionOption.ResponseHeadersRead).AsTask(_cts.Token); // 取消请求的方式改为通过 CancellationTokenSource 来实现了 lblMsg.Text += ((int)response.StatusCode) + " " + response.ReasonPhrase;
lblMsg.Text += Environment.NewLine; // IHttpContent.ReadAsStringAsync() - 获取 string 类型的响应数据
// IHttpContent.ReadAsBufferAsync() - 获取 IBuffer 类型的响应数据
// IHttpContent.ReadAsInputStreamAsync() - 获取 IInputStream 类型的响应数据
using (Stream responseStream = (await response.Content.ReadAsInputStreamAsync()).AsStreamForRead())
{
byte[] buffer = new byte[];
int read = ; while ((read = await responseStream.ReadAsync(buffer, , buffer.Length)) > )
{
lblMsg.Text += "读取的字节数: " + read.ToString();
lblMsg.Text += Environment.NewLine; IBuffer responseBuffer = CryptographicBuffer.CreateFromByteArray(buffer);
lblMsg.Text += CryptographicBuffer.EncodeToHexString(responseBuffer);
lblMsg.Text += Environment.NewLine; buffer = new byte[];
}
}
}
catch (TaskCanceledException)
{
lblMsg.Text += "取消了";
lblMsg.Text += Environment.NewLine;
}
catch (Exception ex)
{
lblMsg.Text += ex.ToString();
lblMsg.Text += Environment.NewLine;
}
} private void btnCancel_Click(object sender, RoutedEventArgs e)
{
// 取消 http 请求
if (_cts != null)
{
_cts.Cancel();
_cts.Dispose();
_cts = null;
}
}
}
}

4、演示 http post string
PostString.xaml.cs

/*
* 演示 http post string
*/ using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using Windows.Web.Http; namespace Windows81.Communication.HTTP
{
public sealed partial class PostString : Page
{
private HttpClient _httpClient;
private CancellationTokenSource _cts; public PostString()
{
this.InitializeComponent();
} protected override void OnNavigatedFrom(NavigationEventArgs e)
{
// 释放资源
if (_httpClient != null)
{
_httpClient.Dispose();
_httpClient = null;
} if (_cts != null)
{
_cts.Dispose();
_cts = null;
}
} private async void btnPostString_Click(object sender, RoutedEventArgs e)
{
_httpClient = new HttpClient();
_cts = new CancellationTokenSource(); try
{
// 需要 post 的数据
var postData = new HttpFormUrlEncodedContent(
new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("param1", "web"),
new KeyValuePair<string, string>("param2", "abcd")
}
); HttpResponseMessage response = await _httpClient.PostAsync(
new Uri("http://localhost:39630/HttpDemo.aspx?action=postString"),
postData).AsTask(_cts.Token); // 取消请求的方式改为通过 CancellationTokenSource 来实现了 lblMsg.Text += ((int)response.StatusCode) + " " + response.ReasonPhrase;
lblMsg.Text += Environment.NewLine; // HttpContent.ReadAsStringAsync() - 以 string 方式获取响应数据
// HttpContent.ReadAsByteArrayAsync() - 以 byte[] 方式获取响应数据
// HttpContent.ReadAsStreamAsync() - 以 stream 方式获取响应数据
lblMsg.Text += await response.Content.ReadAsStringAsync();
lblMsg.Text += Environment.NewLine;
}
catch (TaskCanceledException)
{
lblMsg.Text += "取消了";
lblMsg.Text += Environment.NewLine;
}
catch (Exception ex)
{
lblMsg.Text += ex.ToString();
lblMsg.Text += Environment.NewLine;
}
} private void btnCancel_Click(object sender, RoutedEventArgs e)
{
// 取消 http 请求
if (_cts != null)
{
_cts.Cancel();
_cts.Dispose();
_cts = null;
}
}
}
}

5、演示 http post stream
PostStream.xaml.cs

/*
* 演示 http post stream
*/ using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using Windows.Web.Http; namespace Windows81.Communication.HTTP
{
public sealed partial class PostStream : Page
{
private HttpClient _httpClient;
private CancellationTokenSource _cts; public PostStream()
{
this.InitializeComponent();
} protected override void OnNavigatedFrom(NavigationEventArgs e)
{
// 释放资源
if (_httpClient != null)
{
_httpClient.Dispose();
_httpClient = null;
} if (_cts != null)
{
_cts.Dispose();
_cts = null;
}
} private async void btnPostStream_Click(object sender, RoutedEventArgs e)
{
_httpClient = new HttpClient();
_cts = new CancellationTokenSource(); try
{
// 需要 post 的 stream 数据
Stream stream = GenerateSampleStream();
HttpStreamContent streamContent = new HttpStreamContent(stream.AsInputStream()); HttpResponseMessage response = await _httpClient.PostAsync(
new Uri("http://localhost:39630/HttpDemo.aspx?action=postStream"),
streamContent).AsTask(_cts.Token);// 取消请求的方式改为通过 CancellationTokenSource 来实现了 lblMsg.Text += ((int)response.StatusCode) + " " + response.ReasonPhrase;
lblMsg.Text += Environment.NewLine; // IHttpContent.ReadAsStringAsync() - 获取 string 类型的响应数据
// IHttpContent.ReadAsBufferAsync() - 获取 IBuffer 类型的响应数据
// IHttpContent.ReadAsInputStreamAsync() - 获取 IInputStream 类型的响应数据
lblMsg.Text += await response.Content.ReadAsStringAsync();
lblMsg.Text += Environment.NewLine;
}
catch (TaskCanceledException)
{
lblMsg.Text += "取消了";
lblMsg.Text += Environment.NewLine;
}
catch (Exception ex)
{
lblMsg.Text += ex.ToString();
lblMsg.Text += Environment.NewLine;
}
} // 生成一个指定大小的内存流
private static MemoryStream GenerateSampleStream(int size)
{
byte[] subData = new byte[size];
for (int i = ; i < subData.Length; i++)
{
subData[i] = (byte)( + i % ); // a-z
} return new MemoryStream(subData);
} private void btnCancel_Click(object sender, RoutedEventArgs e)
{
// 取消 http 请求
if (_cts != null)
{
_cts.Cancel();
_cts.Dispose();
_cts = null;
}
}
}
}

OK
[源码下载]

重新想象 Windows 8.1 Store Apps (88) - 通信的新特性: 新的 HttpClient的更多相关文章

  1. 重新想象 Windows 8.1 Store Apps (89) - 通信的新特性: 下载数据, 上传数据, 上传文件

    [源码下载] 重新想象 Windows 8.1 Store Apps (89) - 通信的新特性: 下载数据, 上传数据, 上传文件 作者:webabcd 介绍重新想象 Windows 8.1 Sto ...

  2. 重新想象 Windows 8.1 Store Apps (90) - 通信的新特性: 通过 HttpBaseProtocolFilter 实现 http 请求的缓存控制,以及 cookie 读写; 自定义 HttpFilter; 其他

    [源码下载] 重新想象 Windows 8.1 Store Apps (90) - 通信的新特性: 通过 HttpBaseProtocolFilter 实现 http 请求的缓存控制,以及 cooki ...

  3. 重新想象 Windows 8.1 Store Apps 系列文章索引

    [源码下载] [重新想象 Windows 8 Store Apps 系列文章] 重新想象 Windows 8.1 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...

  4. 重新想象 Windows 8.1 Store Apps (81) - 控件增强: WebView 之加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Contract 分享 WebView 中的内容, 为 WebView 截图

    [源码下载] 重新想象 Windows 8.1 Store Apps (81) - 控件增强: WebView 之加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Co ...

  5. 重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar

    [源码下载] 重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar 作者:webabcd 介绍重新想象 Windows 8.1 Sto ...

  6. 重新想象 Windows 8.1 Store Apps (73) - 新增控件: DatePicker, TimePicker

    [源码下载] 重新想象 Windows 8.1 Store Apps (73) - 新增控件: DatePicker, TimePicker 作者:webabcd 介绍重新想象 Windows 8.1 ...

  7. 重新想象 Windows 8.1 Store Apps (74) - 新增控件: Flyout, MenuFlyout, SettingsFlyout

    [源码下载] 重新想象 Windows 8.1 Store Apps (74) - 新增控件: Flyout, MenuFlyout, SettingsFlyout 作者:webabcd 介绍重新想象 ...

  8. 重新想象 Windows 8.1 Store Apps (75) - 新增控件: Hub, Hyperlink

    [源码下载] 重新想象 Windows 8.1 Store Apps (75) - 新增控件: Hub, Hyperlink 作者:webabcd 介绍重新想象 Windows 8.1 Store A ...

  9. 重新想象 Windows 8.1 Store Apps (76) - 新增控件: SearchBox

    [源码下载] 重新想象 Windows 8.1 Store Apps (76) - 新增控件: SearchBox 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之 ...

随机推荐

  1. 十分钟理解Gradle

    一.什么是Gradle 简单的说,Gradle是一个构建工具,它是用来帮助我们构建app的,构建包括编译.打包等过程.我们可以为Gradle指定构建规则,然后它就会根据我们的“命令”自动为我们构建ap ...

  2. 一个JAVA数据库连接池实现源码

    原文链接:http://www.open-open.com/lib/view/open1410875608164.html // // 一个效果非常不错的JAVA数据库连接池. // from:htt ...

  3. volley中网络请求

    首先使用Volley类创建 RequestQueue queue = Volley.newRequestQueue(this);  Making GET Requests final String u ...

  4. Nginx HTTP负载均衡和反向代理配置

    当前大并发的网站基本都采用了Nginx来做代理服务器,并且做缓存,来扛住大并发.先前也用nginx配置过简单的代理,今天有时间把整合过程拿出来和大家分享,不过其中大部分也是网上找来的资源. nginx ...

  5. 打开 Mac OS X 隐藏的用户资源库文件夹的方法

    在较高版本的 Mac OS X 中,用户的资源库文件夹(/Users/username/Library)默认被系统隐藏了,从 Finder 窗口中不能直接打开. 但是通过下面这个方法可以快速打开用户资 ...

  6. Frogger(floyd变形)

    Frogger Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  7. SQL Server 2008 Windows身份验证改为混合模式身份验证

    1.在当前服务器右键进入“属性页”->“安全性”->勾选Sql Server和Windows身份验证模式->确定. 由于默认不启用sa,所以如果启用sa账户登录,则还需要如下设置: ...

  8. BIEE安装文件下载地址

    Repository Creation Utility 下载地址 http://www.oracle.com/technetwork/middleware/bi-enterprise-edition/ ...

  9. 组合模式及C++实现

    组合模式 组合模式,是为了解决整体和部分的一致对待的问题而产生的,要求这个整体与部分有一致的操作或行为.部分和整体都继承与一个公共的抽象类,这样,外部使用它们时是一致的,不用管是整体还是部分,使用一个 ...

  10. log4net日志功能使用

        早就想了解下log4net这个组件的使用,直至今日才有时间学习了一下,现在把学习的新的总结如下: (1).在项目中添加log4net.dll引用.说明:该版本是1.2.10.0 ,log4ne ...