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#微信开发小白成长教程二(新手接入指南,附视频)
距离第一讲又已经过去了一个多星期了,本打算一周更新一讲的,奈何实在太忙.最近也在群里发现有一部分人已经可以熟练调用微信的部分接口但却不是很清楚微信公众平台接收消息的一个处理机制.本讲就来介绍下怎么接入 ...
随机推荐
- [记录]ASP.NET MVC 2.0 如何使用Html.RadioButtonFor?
在MVC 2.0里支持强类型实体绑定,可以直接使用如 <%: Html.TextBoxFor(model => model.Description, new { @class=" ...
- row_number()over(partition by 字段 order by 字段)ID,修改重复行的字段值。
案例分析: 现在要查询一个表单里面的运费结果,但是他还有分录,为了显示分录,必须把表头显示出来,问题是,他要查询运费的合计, 但是这样就会导致重复行也加进去了,这样显然数据不准,为此,可以把重复的行设 ...
- php常见知识
printf("%.nf",&f);这个n代表显示浮点数时,小数点后显示几位:0就是不显示小数点后的数,1就是显示小数点后1位:
- FMDB处理动态插入语句
昨天做一个需求,参数的数量不确定,所以无法使用这个API: - (BOOL)executeUpdate:(NSString*)sql, ... 但是用 - (BOOL)executeUpdate:(N ...
- Linux shell基础
shell是核心程序kernel之外的指令解析器,是一个程序,同事是一种命令语言和程序设计语言 --shell是命令解析器,用户输入命令,它去解析. shell类型 ash,bash,ksh,csh, ...
- tkinter 类继承的三种方式
tkinter class继承有三种方式. 提醒注意这几种继承的运行方式 一.继承 object 1.铺tk.Frame给parent: 说明: self.rootframe = tk.Frame(p ...
- opencv4-highgui之视频的输入和输出以及滚动条
这是<opencv2.4.9tutorial.pdf>的highgui的三个例子.通过简短的介绍来实现不同函数的理解,省去一些不需要说的东西. 一.增加滑动条 这是opencv中为数不多的 ...
- 《深入理解Spark:核心思想与源码分析》(第2章)
<深入理解Spark:核心思想与源码分析>一书前言的内容请看链接<深入理解SPARK:核心思想与源码分析>一书正式出版上市 <深入理解Spark:核心思想与源码分析> ...
- 使用Python 将shapefile导入mongodb
使用Python 将shapefile导入mongodb 随着big data时代的到来,各个行业都在考虑能不能把big data的思路.方法引入进来,GIS行业也不能免俗. 下面就介绍一下如何将sh ...
- [CF #236 (Div. 2) E] Strictly Positive Matrix(强联通分量)
题目:http://codeforces.com/contest/402/problem/E 题意:给你一个矩阵a,判断是否存在k,使得a^k这个矩阵全部元素都大于0 分析:把矩阵当作01矩阵,超过1 ...