C#微信开发之旅(二):基础类之HttpClientHelper(更新:SSL安全策略)
public class HttpClientHelper 2 { 3 /// <summary> 4 /// get请求 5 /// </summary> 6 /// <param name="url"></param> 7 /// <returns></returns> 8 public static string GetResponse(string url) 9 { 10 if (url.StartsWith("https")) 11 System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; 12 13 HttpClient httpClient = new HttpClient(); 14 httpClient.DefaultRequestHeaders.Accept.Add( 15 new MediaTypeWithQualityHeaderValue("application/json")); 16 HttpResponseMessage response = httpClient.GetAsync(url).Result; 17 18 if (response.IsSuccessStatusCode) 19 { 20 string result = response.Content.ReadAsStringAsync().Result; 21 return result; 22 } 23 return null; 24 } 25 26 public static T GetResponse<T>(string url) 27 where T : class,new() 28 { 29 if (url.StartsWith("https")) 30 System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; 31 32 HttpClient httpClient = new HttpClient(); 33 httpClient.DefaultRequestHeaders.Accept.Add( 34 new MediaTypeWithQualityHeaderValue("application/json")); 35 HttpResponseMessage response = httpClient.GetAsync(url).Result; 36 37 T result = default(T); 38 39 if (response.IsSuccessStatusCode) 40 { 41 Task<string> t = response.Content.ReadAsStringAsync(); 42 string s = t.Result; 43 44 result = JsonConvert.DeserializeObject<T>(s); 45 } 46 return result; 47 } 48 49 /// <summary> 50 /// post请求 51 /// </summary> 52 /// <param name="url"></param> 53 /// <param name="postData">post数据</param> 54 /// <returns></returns> 55 public static string PostResponse(string url, string postData) 56 { 57 if (url.StartsWith("https")) 58 System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; 59 60 HttpContent httpContent = new StringContent(postData); 61 httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); 62 HttpClient httpClient = new HttpClient(); 63 64 HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result; 65 66 if (response.IsSuccessStatusCode) 67 { 68 string result = response.Content.ReadAsStringAsync().Result; 69 return result; 70 } 71 return null; 72 } 73 74 /// <summary> 75 /// 发起post请求 76 /// </summary> 77 /// <typeparam name="T"></typeparam> 78 /// <param name="url">url</param> 79 /// <param name="postData">post数据</param> 80 /// <returns></returns> 81 public static T PostResponse<T>(string url, string postData) 82 where T : class,new() 83 { 84 if (url.StartsWith("https")) 85 System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; 86 87 HttpContent httpContent = new StringContent(postData); 88 httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); 89 HttpClient httpClient = new HttpClient(); 90 91 T result = default(T); 92 93 HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result; 94 95 if (response.IsSuccessStatusCode) 96 { 97 Task<string> t = response.Content.ReadAsStringAsync(); 98 string s = t.Result; 99100 result = JsonConvert.DeserializeObject<T>(s);101 }102 return result;103 }104105 /// <summary>106 /// V3接口全部为Xml形式,故有此方法107 /// </summary>108 /// <typeparam name="T"></typeparam>109 /// <param name="url"></param>110 /// <param name="xmlString"></param>111 /// <returns></returns>112 public static T PostXmlResponse<T>(string url, string xmlString)113 where T : class,new()114 {115 if (url.StartsWith("https"))116 System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;117118 HttpContent httpContent = new StringContent(xmlString);119 httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");120 HttpClient httpClient = new HttpClient();121122 T result = default(T);123124 HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;125126 if (response.IsSuccessStatusCode)127 {128 Task<string> t = response.Content.ReadAsStringAsync();129 string s = t.Result;130131 result = XmlDeserialize<T>(s);132 }133 return result;134 }135136 /// <summary>137 /// 反序列化Xml138 /// </summary>139 /// <typeparam name="T"></typeparam>140 /// <param name="xmlString"></param>141 /// <returns></returns>142 public static T XmlDeserialize<T>(string xmlString) 143 where T : class,new ()144 {145 try146 {147 XmlSerializer ser = new XmlSerializer(typeof(T));148 using (StringReader reader = new StringReader(xmlString))149 {150 return (T)ser.Deserialize(reader);151 }152 }153 catch (Exception ex)154 {155 throw new Exception("XmlDeserialize发生异常:xmlString:" + xmlString + "异常信息:" + ex.Message);156 }157158 }159 }C#微信开发之旅(二):基础类之HttpClientHelper(更新:SSL安全策略)的更多相关文章
- C#微信开发之旅--自定义菜单
上一篇说道基本信息的回复<C#微信开发之旅--基本信息的回复>,当中就说到文本信息的回复,其他信息的回复,可以参考下开发文档中回复信息的格式进行修改就可以. 下面来实现下自定义菜单.据我了 ...
- C#微信开发之旅--基本信息的回复
上一篇说到配置和验证<C#微信开发之旅--准备阶段> 下面来实现一下简单的信息回复. 也就是接收XML,返回XML 可以去看下微信开发文档的说明:http://mp.weixin.qq.c ...
- C#微信开发之旅--准备阶段
最近才开始学微信开发的相关内容,记录下,慢慢的养成习惯! 1.申请公众号: 公众号分为 订阅号 和 服务号.他们之前的区别可以点击这里查看 因为我们是测试的,所以可以直接申请测试帐号,就把所有的功能都 ...
- 微信开发 企业号(二)-- 回调模式之Tooken验证 .net/python
在企业号开发者中心中,有加密解密源代码,供给开发者使用.(加解密库下载) 由于官方只提供了python2.*的类库,使用python3.*的朋友可以再最后下载我修改后的py文件(仅修改验证Tooken ...
- Force.com微信开发系列(二)用户消息处理
Force.com是国际知名的云平台公司,成功配置好Force.com作为微信公开号的服务端后,接下来需要的任务是处理用户发送的消息.当普通微信用户向公众账号发消息时,微信服务器将POST消息的XML ...
- QT开发之旅二TCP调试工具
TCP调试工具顾名思义用来调试TCP通信的,网上这样的工具N多,之前用.NET写过一个,无奈在XP下还要安装个.NET框架才能运行,索性这次用QT重写,发现QT写TCP通信比.NET还要便捷一些,运行 ...
- 网站实现微信登录之嵌入二维码——基于yii2开发的描述
之前写了一篇yii2获取登录前的页面url地址的文章,然后发现自己对于网站实现微信扫码登录功能的实现不是很熟悉,所以,我会写2-3篇的文章来描述下一个站点如何实现微信扫码登录的功能,来复习下微信扫码登 ...
- C#微信公众号开发系列教程二(新手接入指南)
http://www.cnblogs.com/zskbll/p/4093954.html 此系列前面已经更新了两篇博文了,都是微信开发的前期准备工作,现在切入正题,本篇讲解新手接入的步骤与方法,大神可 ...
- C#微信开发小白成长教程二(新手接入指南,附视频)
距离第一讲又已经过去了一个多星期了,本打算一周更新一讲的,奈何实在太忙.最近也在群里发现有一部分人已经可以熟练调用微信的部分接口但却不是很清楚微信公众平台接收消息的一个处理机制.本讲就来介绍下怎么接入 ...
随机推荐
- linux进入软连接所指向的原目录
软连接就是一个快捷方式,建立软连接的方法 ln -s source-path-or-file link-file 建立硬连接 ln source-path-or-file link-file linu ...
- 逗号分隔的字符串转换为行数据(collection)(续)
逗号分隔的字符串转行数据的存储过程一个: CREATE OR REPLACE FUNCTION SP_YX_SPLIT ( p_list CLOB, p_sep VARCHAR2 := ',' ) R ...
- Nginx 使用IP限制访问来源
在 server {... 下, 或者在 location xxx {... 下, 都可以添加如下的IP访问限制 allow 10.57.22.172; allow ; allow ; allow ; ...
- Oracle 11g XE release2安装与指导
今天上午我安装了Oracle 11g企业版,发现太占内存了,考虑到MS SQL有express版本,所以寻思着尝试尝试Oracle 11g的express版本,就是EX版本.下面是具体的安装步骤. 1 ...
- win10显示此电脑
http://jingyan.baidu.com/article/3aed632e00dfe17011809169.html
- subtable
- css3在不同型号手机浏览器上的兼容一览表
网上搜集了css3对不同系统手机浏览器的支持情况(ios/android/winphone)备份一下以便查看. 以下资料由微信产品部"白树"整理, 转载请注明. √:完全支持 ...
- Cannot resolve external dependency com.android.support:multidex:1.0.0
multiDexEnabled true 去掉这一行,就OK了,是没有多dex支持么,但是又提供了这个
- zepto笔记 001
$(function(){}) 在页面加载完成后运行的方法 等于window.onload; $("#id"),$(this) 都和jquery一样, tap方法不能阻止事件冒泡, ...
- noi题库(noi.openjudge.cn) 1.9编程基础之顺序查找T01——T05
T01 查找特定元素的值 描述 在一个序列(下标从1开始)中查找一个给定的值,输出第一次出现的位置. 输入 第一行包含一个正整数n,表示序列中元素个数.1 <= n <= 10000.第二 ...