【Windows 8 Store App】学习三:HTTP

简介: 原文 http://www.cnblogs.com/java-koma/archive/2013/05/22/3093309.html 1,HttpClient Win 8提供了System.Net.Http.HttpClient类进行常用的http网络请求,HttpClient提供了以下构造函数。

原文 http://www.cnblogs.com/java-koma/archive/2013/05/22/3093309.html

1,HttpClient

Win 8提供了System.Net.Http.HttpClient类进行常用的http网络请求,HttpClient提供了以下构造函数。

		// 摘要:
		//     初始化 System.Net.Http.HttpClient 类的新实例。
		public HttpClient();
		//
		// 摘要:
		//     用特定的处理程序初始化 System.Net.Http.HttpClient 类的新实例。
		//
		// 参数:
		//   handler:
		//     用于发送请求的使用的 HTTP 处理程序堆栈。
		public HttpClient(HttpMessageHandler handler);
		//
		// 摘要:
		//     用特定的处理程序初始化 System.Net.Http.HttpClient 类的新实例。
		//
		// 参数:
		//   handler:
		//     System.Net.Http.HttpMessageHandler 负责处理 HTTP 响应消息。
		//
		//   disposeHandler:
		//     如果内部处理程序应由 Dispose () 处理,则为 true;如果您希望重用内部处理程序,则为 false。
		public HttpClient(HttpMessageHandler handler, bool disposeHandler);

 

第2个构造函数常用来处理在请求前添加header(如:Cookie),响应时解析header。

下面使用HttpClient处理POST/GET提交:

#1. 让我们先来定义好key-value类型的参数,用于提交。

	public class Parameter
	{
		public string key { get; set; }
		public string value { get; set; }

		public Parameter() { }
		public Parameter(string key, string value)
		{
			this.key = key;
			this.value = value;
		}
	}

 

#2. POST/GET:

		private static async Task<string> doRequest<T>(string url, List<Parameter> paramList, bool isPost)
		{
			System.Net.Http.HttpClient httpClient = null;
			try
			{
				httpClient = new System.Net.Http.HttpClient();
				httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
				HttpResponseMessage response = null;
				// POST
				if (isPost)
				{
					MultipartFormDataContent form = getPostForm(paramList);
					response = await httpClient.PostAsync(new Uri(url), form);
				}
				// GET
				else
				{
					url = generateGetUrl(url, paramList);
					response = await httpClient.GetAsync(new Uri(url));
				}
				return await response.Content.ReadAsStringAsync();
			}
			catch (Exception) { }
			finally
			{
				if (httpClient != null)
				{
					httpClient.Dispose();
					httpClient = null;
				}
			}
			return null;
		}
		private static string generateGetUrl(string url, List<Parameter> paramList)
		{
			if(paramList == null || paramList.Count <= 0)
			{
				return url;
			}
			StringBuilder sb = new StringBuilder();
			foreach (Parameter item in this.ParamList)
			{
				if (item == null || string.IsNullOrWhiteSpace(item.key) || string.IsNullOrWhiteSpace(item.value))
				{
					continue;
				}
				if (sb.Length > 0)
				{
					sb.Append("&");
				}
				sb.Append(string.Format("{0}={1}", item.key, System.Net.WebUtility.UrlEncode(item.value)));
			}
			return url + (url.IndexOf("?") == -1 ? "?" : "&") + sb.ToString();
		}

		private static MultipartFormDataContent getPostForm(List<Parameter> paramList) 
		{
			MultipartFormDataContent form = new MultipartFormDataContent();
			if (paramList != null)
			{
				foreach (var param in paramList)
				{
					if (!string.IsNullOrWhiteSpace(param.key))
					{
						form.Add(new StringContent(param.value, UTF8Encoding.UTF8), param.key);
					}
				}
			}
			return form;
		}


#3. 处理Cookie,

通常情况下我们需要保持client与server之间的session,server端是通过cookie来识别一个client与另外一个client的。

我们使用上面HttpClient的第2个构造函数,通过MessageProcessingHandler和CookieContainer来每次请求前,把cookie添加到request的header中。

	public class CookieHandler : MessageProcessingHandler
	{
		static CookieHandler()
        {
            CookieContainer = new CookieContainer();
        }

        public static CookieContainer CookieContainer
        {
            get;
            set;
        }

		public CookieHandler() : base(new CookieHttpClientHandler())
        {
        }

		protected override HttpRequestMessage ProcessRequest(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
		{
			return request;
		}

		protected override HttpResponseMessage ProcessResponse(HttpResponseMessage response, System.Threading.CancellationToken cancellationToken)
		{
			Uri httpsUri = new Uri("https://" + response.RequestMessage.RequestUri.Host);

			var cookieCollection = CookieContainer.GetCookies(httpsUri);
			foreach (Cookie cookie in cookieCollection)
			{
				cookie.Secure = false;
			}

			return response;
		}
	}

	class CookieHttpClientHandler : HttpClientHandler 
	{
		public CookieHttpClientHandler() 
		{
			CookieContainer = CookieHandler.CookieContainer;
		}
	}


使用方式跟前面POST/GET代码唯一不同的是:在构造HttpClient对象时,传入CookieHandler:

var httpClient = new System.Net.Http.HttpClient(new CookieHandler());


2, 文件下载

#1 HttpClient提供了字节流的方式来读取文件,但我测试发现,下载是成功了,但文件经常出现缺少字节的情况。不清楚是怎么回事。

		//
		// 摘要:
		//     将 GET 请求发送到指定 URI 并在异步操作中以字节数组的形式返回响应正文。
		//
		// 参数:
		//   requestUri:
		//     请求发送到的 URI。
		//
		// 返回结果:
		//     返回 System.Threading.Tasks.Task<TResult>。 表示异步操作的任务对象。
		//
		// 异常:
		//   System.ArgumentNullException:
		//     requestUri 为 null。
		public Task<byte[]> GetByteArrayAsync(string requestUri);
		//
		// 摘要:
		//     将 GET 请求发送到指定 URI 并在异步操作中以字节数组的形式返回响应正文。
		//
		// 参数:
		//   requestUri:
		//     请求发送到的 URI。
		//
		// 返回结果:
		//     返回 System.Threading.Tasks.Task<TResult>。 表示异步操作的任务对象。
		//
		// 异常:
		//   System.ArgumentNullException:
		//     requestUri 为 null。
		public Task<byte[]> GetByteArrayAsync(Uri requestUri);
		//
		// 摘要:
		//     将 GET 请求发送到指定 URI 并在异步操作中以流的形式返回响应正文。
		//
		// 参数:
		//   requestUri:
		//     请求发送到的 URI。
		//
		// 返回结果:
		//     返回 System.Threading.Tasks.Task<TResult>。 表示异步操作的任务对象。
		//
		// 异常:
		//   System.ArgumentNullException:
		//     requestUri 为 null。
		public Task<System.IO.Stream> GetStreamAsync(string requestUri);
		//
		// 摘要:
		//     将 GET 请求发送到指定 URI 并在异步操作中以流的形式返回响应正文。
		//
		// 参数:
		//   requestUri:
		//     请求发送到的 URI。
		//
		// 返回结果:
		//     返回 System.Threading.Tasks.Task<TResult>。 表示异步操作的任务对象。
		//
		// 异常:
		//   System.ArgumentNullException:
		//     requestUri 为 null。
		public Task<System.IO.Stream> GetStreamAsync(Uri requestUri);

 

#2. BackgroundDownloader

Win 8提供了BackgroundDownloader可以用于在后台下载文件,它也可以调用setRequestHeader向请求中添加header信息 (与上面的CookieHandler结合使用,可以处理那些需要登陆才能下载文件的情况),下面演示了普通的文件下载:

		public async static Task<IAsyncOperation<StorageFile>> DownloadAsync(string url)
		{
			string fileName = url.Substring(url.LastIndexOf("/") + 1).Trim();
			var option = Windows.Storage.CreationCollisionOption.ReplaceExisting;
			StorageFile destinationFile = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, option);
			BackgroundDownloader downloader = new BackgroundDownloader();
			DownloadOperation download = downloader.CreateDownload(new Uri(url), destinationFile);
			await download.StartAsync().AsTask();
			ResponseInformation response = download.GetResponseInformation();
			if(response.StatusCode == 200)
			{
				DownloadHelper.addDownloadFileSuccess(fileName);
				return DownloadHelper.getDownloadFileAsync(fileName);
			}
			return null;
		}
目录
相关文章
|
8月前
|
运维 iOS开发 Windows
windows电脑备案ios APP获取公钥和证书指纹Sha-1值的方法
在阿里云进行APP备案、在备案IOS端的环节的时候,发现需要我们将p12证书安装在电脑上,再用xcode或或钥匙串访问来获取这个证书的公钥和sha-1值。 但是大部分开发uniapp应用的同学们,或者进行发布的运维人员的电脑都是windows,无法按照阿里云的教程来获取ios的公钥和sha-1。备案就被卡主了。 这里介绍下另一个方法,就是使用香蕉云编来在线上传证书获取。如下图所示,打开香蕉云编后,找到下图这个功能
1198 0
|
Java Maven Kotlin
vertx的学习总结7之用kotlin 与vertx搞一个简单的http
本文介绍了如何使用Kotlin和Vert.x创建一个简单的HTTP服务器,包括设置路由、处理GET和POST请求,以及如何使用HTML表单发送数据。
269 2
vertx的学习总结7之用kotlin 与vertx搞一个简单的http
|
C# Windows
【Azure App Service】在App Service for Windows上验证能占用的内存最大值
根据以上测验,当使用App Service内存没有达到预期的值,且应用异常日志出现OutOfMemory时,就需要检查Platform的设置是否位64bit。
336 11
|
Java 应用服务中间件 开发工具
[App Service for Windows]通过 KUDU 查看 Tomcat 配置信息
[App Service for Windows]通过 KUDU 查看 Tomcat 配置信息
249 2
|
Java 应用服务中间件 Windows
【App Service for Windows】为 App Service 配置自定义 Tomcat 环境
【App Service for Windows】为 App Service 配置自定义 Tomcat 环境
206 2
|
PHP Windows
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
361 1
|
API 开发工具 UED
在 UWP 中使用 Windows App SDK
【10月更文挑战第17天】在UWP中使用Windows App SDK可增强应用功能和性能。首先了解SDK特性,接着安装Visual Studio 2022及以上版本,并从微软官网下载安装SDK。配置项目时,确保目标版本支持SDK,添加SDK引用后即可使用新API提升应用体验。开发过程中应充分利用调试工具进行测试,确保应用的兼容性和稳定性。
427 0
|
网络安全 API 数据安全/隐私保护
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
253 0
|
API C#
【Azure App Service】验证App Service接受HTTP 2.0请求
【Azure App Service】验证App Service接受HTTP 2.0请求
170 0
|
Shell PHP Windows
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
287 0