[源码下载]

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

作者:webabcd

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

  • 下载数据(显示下载进度,将下载数据保存到本地)
  • 上传数据(显示上传进度)
  • 上传文件

示例
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(Windows.Web.Http)获取下载进度,并将下载数据保存到本地
Download.xaml.cs

/*
* 本例演示如何通过新的 HttpClient(Windows.Web.Http)获取下载进度,并将下载数据保存到本地
*
*
* 注:在 win8 时代要想获取下载进度只能依靠后台任务来完成
*/ using System;
using System.Threading;
using System.Threading.Tasks;
using Windows.Storage;
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 Download : Page
{
private HttpClient _httpClient;
private CancellationTokenSource _cts; public Download()
{
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 btnDownload_Click(object sender, RoutedEventArgs e)
{
_httpClient = new HttpClient();
_cts = new CancellationTokenSource(); try
{
// 用于获取下载进度
IProgress<HttpProgress> progress = new Progress<HttpProgress>(ProgressHandler); HttpResponseMessage response = await _httpClient.GetAsync(
new Uri("http://files.cnblogs.com/webabcd/WindowsPhone.rar?ll"),
HttpCompletionOption.ResponseContentRead).AsTask(_cts.Token, progress); // 把 progress 放到 task 里,以便获取下载进度 lblMsg.Text += ((int)response.StatusCode) + " " + response.ReasonPhrase;
lblMsg.Text += Environment.NewLine; // 将下载好的数据保存到本地
StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
StorageFile storageFile = await storageFolder.CreateFileAsync("WindowsPhone.rar", CreationCollisionOption.ReplaceExisting);
using (StorageStreamTransaction transaction = await storageFile.OpenTransactedWriteAsync())
{
lblMsg.Text = "文件已下载,写入到磁盘中..."; /*
* IHttpContent.WriteToStreamAsync() - 用于保存数据
*/
await response.Content.WriteToStreamAsync(transaction.Stream);
await transaction.CommitAsync(); lblMsg.Text = "文件已写入到磁盘";
}
}
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;
}
} // 下载进度发生变化时调用的处理器
private void ProgressHandler(HttpProgress progress)
{
/*
* HttpProgress - http 通信的进度
* BytesReceived - 已收到的字节数
* BytesSent - 已发送的字节数
* TotalBytesToReceive - 总共需要收到的字节数
* TotalBytesToSend - 总共需要发送的字节数
* Retries - 重试次数
* Stage - 当前通信的阶段(HttpProgressStage 枚举)
*/ string result = "BytesReceived: {0}\nBytesSent: {1}\nRetries: {2}\nStage: {3}\nTotalBytesToReceive: {4}\nTotalBytesToSend: {5}\n";
result = string.Format(result, progress.BytesReceived, progress.BytesSent, progress.Retries, progress.Stage, progress.TotalBytesToReceive, progress.TotalBytesToSend); lblMsg.Text = result;
}
}
}

2、演示如何通过新的 HttpClient(Windows.Web.Http)获取上传进度
Upload.xaml.cs

/*
* 本例演示如何通过新的 HttpClient(Windows.Web.Http)获取上传进度
*
*
* 注:在 win8 时代要想获取上传进度只能依靠后台任务来完成
*/ using System;
using System.IO;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading;
using System.Threading.Tasks;
using Windows.Foundation;
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 Upload : Page
{
private HttpClient _httpClient;
private CancellationTokenSource _cts; public Upload()
{
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 btnUpload_Click(object sender, RoutedEventArgs e)
{
_httpClient = new HttpClient();
_cts = new CancellationTokenSource(); try
{
Uri resourceAddress = new Uri("http://localhost:39630/HttpDemo.aspx?action=postStream"); // 模拟一个比较大的比较慢的流,供 http 上传
const uint streamLength = ;
HttpStreamContent streamContent = new HttpStreamContent(new SlowInputStream(streamLength));
streamContent.Headers.ContentLength = streamLength; // 必须要指定请求数据的 ContentLength,否则就是 chunked 了 // 用于获取上传进度
IProgress<HttpProgress> progress = new Progress<HttpProgress>(ProgressHandler); HttpResponseMessage response = await _httpClient.PostAsync(resourceAddress, streamContent).AsTask(_cts.Token, progress); // 把 progress 放到 task 里,以便获取上传进度 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 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;
}
} // 上传进度发生变化时调用的处理器
private void ProgressHandler(HttpProgress progress)
{
/*
* HttpProgress - http 通信的进度
* BytesReceived - 已收到的字节数
* BytesSent - 已发送的字节数
* TotalBytesToReceive - 总共需要收到的字节数
* TotalBytesToSend - 总共需要发送的字节数
* Retries - 重试次数
* Stage - 当前通信的阶段(HttpProgressStage 枚举)
*/ string result = "BytesReceived: {0}\nBytesSent: {1}\nRetries: {2}\nStage: {3}\nTotalBytesToReceive: {4}\nTotalBytesToSend: {5}\n";
result = string.Format(result, progress.BytesReceived, progress.BytesSent, progress.Retries, progress.Stage, progress.TotalBytesToReceive, progress.TotalBytesToSend); lblMsg.Text = result;
}
} // 模拟一个比较慢的输入流
class SlowInputStream : IInputStream
{
uint length;
uint position; public SlowInputStream(uint length)
{
this.length = length;
position = ;
} public IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options)
{
return AsyncInfo.Run<IBuffer, uint>(async (cancellationToken, progress) =>
{
if (length - position < count)
{
count = length - position;
} byte[] data = new byte[count];
for (uint i = ; i < count; i++)
{
data[i] = ;
} // 延迟 10 毫秒再继续,以模拟一个比较慢的输入流
await Task.Delay(); position += count;
progress.Report(count); return data.AsBuffer();
});
} public void Dispose()
{ }
}
}

3、演示如何通过新的 HttpClient(Windows.Web.Http)上传文件(通过 multipart/form-data 的方式)
UploadFile.xaml.cs

/*
* 本例演示如何通过新的 HttpClient(Windows.Web.Http)上传文件(通过 multipart/form-data 的方式)
*/ using System;
using System.Threading;
using System.Threading.Tasks;
using Windows.Storage;
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 UploadFile : Page
{
private HttpClient _httpClient;
private CancellationTokenSource _cts; public UploadFile()
{
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 btnUploadFile_Click(object sender, RoutedEventArgs e)
{
_httpClient = new HttpClient();
_cts = new CancellationTokenSource(); try
{
// 构造需要上传的文件数据
StorageFile file1 = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Son.jpg", UriKind.Absolute));
IRandomAccessStreamWithContentType stream1 = await file1.OpenReadAsync();
HttpStreamContent streamContent1 = new HttpStreamContent(stream1); // 构造需要上传的文件数据
StorageFile file2 = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Son.jpg", UriKind.Absolute));
IRandomAccessStreamWithContentType stream2 = await file1.OpenReadAsync();
HttpStreamContent streamContent2 = new HttpStreamContent(stream2); // 通过 HttpMultipartFormDataContent 来指定需要“multipart/form-data”上传的文件
HttpMultipartFormDataContent fileContent = new HttpMultipartFormDataContent();
// 第 1 个参数:需要上传的文件数据
// 第 2 个参数:对应 asp.net 服务的 Request.Files 中的 key(参见:WebServer 项目中的 HttpDemo.aspx.cs)
// 第 3 个参数:对应 asp.net 服务的 Request.Files 中的 fileName(参见:WebServer 项目中的 HttpDemo.aspx.cs)
fileContent.Add(streamContent1, "file1", "file1.jpg");
fileContent.Add(streamContent2, "file2", "file2.jpg"); HttpResponseMessage response = await _httpClient.PostAsync(new Uri("http://localhost:39630/HttpDemo.aspx?action=uploadFile"), fileContent).AsTask(_cts.Token); 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;
}
}
}
}

OK
[源码下载]

重新想象 Windows 8.1 Store Apps (89) - 通信的新特性: 下载数据, 上传数据, 上传文件的更多相关文章

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

    [源码下载] 重新想象 Windows 8.1 Store Apps (88) - 通信的新特性: 新的 HttpClient 作者:webabcd 介绍重新想象 Windows 8.1 Store ...

  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 (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性

    [源码下载] 重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性 作者:webabcd 介 ...

  4. 重新想象 Windows 8.1 Store Apps (83) - 文件系统的新特性

    [源码下载] 重新想象 Windows 8.1 Store Apps (83) - 文件系统的新特性 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之文件系统的新特 ...

  5. 重新想象 Windows 8.1 Store Apps (84) - 图像处理的新特性, Share Contract 的新特性

    [源码下载] 重新想象 Windows 8.1 Store Apps (84) - 图像处理的新特性, Share Contract 的新特性 作者:webabcd 介绍重新想象 Windows 8. ...

  6. 重新想象 Windows 8.1 Store Apps (80) - 控件增强: WebView 之基本应用, POST 数据, 与 JavaScript 交互

    [源码下载] 重新想象 Windows 8.1 Store Apps (80) - 控件增强: WebView 之基本应用, POST 数据, 与 JavaScript 交互 作者:webabcd 介 ...

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

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

  8. 重新想象 Windows 8.1 Store Apps (85) - 警报通知(闹钟), Tile 的新特性

    [源码下载] 重新想象 Windows 8.1 Store Apps (85) - 警报通知(闹钟), Tile 的新特性 作者:webabcd 介绍重新想象 Windows 8.1 Store Ap ...

  9. 重新想象 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 ...

随机推荐

  1. U-boot.lds文件分析

    OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm") ...

  2. Android使用BLE(低功耗蓝牙,Bluetooth Low Energy)

    背景 在学习BLE的过程中,积累了一些心得的DEMO,放到Github,形成本文.感兴趣的同学可以下载到源代码. github: https://github.com/vir56k/bluetooth ...

  3. [原创]Android从xml加载到View对象过程解析

    我们从Activity的setContentView()入手,开始源码解析, //Activity.setContentView public void setContentView(int layo ...

  4. 解决HP打印机错误:Couldn't open fifo

    我的是因为选错了打印机协议,一开始选成了“互联网打印协议 - IPP”. 解决方案:删除原有打印机配置,重新选择协议为“HP Jetdirect-Socket”即可.

  5. 让IE8支持HTML5及canvas功能!

    微软出的IE9支持HTML5,但因为不支持XP系统,暂时我还用不了. 即使能用,现阶段如果开发HTML5页面,并考虑到兼容性问题的话,恐怕也得让自己的界面支持IE6-8吧. 首先,需要让IE支持HTM ...

  6. web通过ActiveX打印

    最近做了一个activex控件,可以通过html页面动态设置报表文件的数据,控件里的报表是通过FastReport实现了,可以通过FastReport先把报表设置好.欢迎大家提意见 控件及Demo下载

  7. 十八、【开源】EnterpriseFrameWork框架核心类库之Winform控制器

    回<[开源]EnterpriseFrameWork框架系列文章索引> EFW框架源代码下载:http://pan.baidu.com/s/1qWJjo3U EFW框架中的WinContro ...

  8. Android 2.1 和 Android 4.4 工程目录超详细对比及详解

    在搭建Android开发环境及简单地建立一个HelloWorld项目后,本篇将通过HelloWorld项目来介绍Android项目的目录结构.本文的主要主题如下: 1.1.HelloWorld项目的目 ...

  9. cocos2d-x开发: 场景实体(entity)管理

    公司现在开新项目,主题的框架部分都是我自己在做,不用受到别人的牵制,所以还算是比较的自由,很好发挥. 游戏并不大,所以需要用到的地方并不多.今天花了一些时间写了场景entity管理的部分代码,还没有完 ...

  10. Mybatis对MySQL中BLOB字段的读取

    1.在sqlMapConfig中,定义一个typeHandlers <typeHandlers> <typeHandler jdbcType="BLOB" jav ...