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;
99
100
result = JsonConvert.DeserializeObject<T>(s);
101
}
102
return
result;
103
}
104
105
/// <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;
117
118
HttpContent httpContent =
new
StringContent(xmlString);
119
httpContent.Headers.ContentType =
new
MediaTypeHeaderValue(
"application/json"
);
120
HttpClient httpClient =
new
HttpClient();
121
122
T result =
default
(T);
123
124
HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
125
126
if
(response.IsSuccessStatusCode)
127
{
128
Task<string> t = response.Content.ReadAsStringAsync();
129
string s = t.Result;
130
131
result = XmlDeserialize<T>(s);
132
}
133
return
result;
134
}
135
136
/// <summary>
137
/// 反序列化Xml
138
/// </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
try
146
{
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
}
157
158
}
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#微信开发小白成长教程二(新手接入指南,附视频)
距离第一讲又已经过去了一个多星期了,本打算一周更新一讲的,奈何实在太忙.最近也在群里发现有一部分人已经可以熟练调用微信的部分接口但却不是很清楚微信公众平台接收消息的一个处理机制.本讲就来介绍下怎么接入 ...
随机推荐
- sublime3 常用插件
1,emmet,html代码自动补全插件 2,jsFormat js格式化插件 3,HTMLBeautify 格式化html插件 4,autoPrefix css3自动补全前缀 5,SublimeCo ...
- JS中NULL和Undefined的区别
NULL是表示一个”无“的对象,转换成数值为0:undefined是一个“无”的原始值,转为数值为NaN: 当声明的变量还未被初始化时,变量的默认值为undefined: null用来表示尚未存在的对 ...
- 使用Jquery向一个空白网页动态创建一个iframe,及嵌入页面,和向嵌入页面传参
[csharp] view plaincopyprint?using Microsoft.VisualBasic; using System; using System.Collections; us ...
- VFS分析(二)基本数据结构(持续更新)
nameidata /mnt/dir1/dir2/ nameidata结构体是一个临时的结构体, 目标是为了找到最后的dentry.
- 4829 [DP]数字三角形升级版
4829 [DP]数字三角形升级版 时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 从数字三角形的顶部(如图, ...
- codevs 2801 LOL-盖伦的蹲草计划
时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题目描述 Description 众所周知,LOL这款伟大的游戏,有个叫盖伦的英雄.他的伟大之处在于他特别喜欢蹲 ...
- (转)DataMatrix编码2——伽罗华域运算
原文出处:http://blog.sina.com.cn/s/blog_4572df4e01019wsj.html 伽罗华域即有限域,RS编码在此域中进行运算,故不得不对其有所了解.DataMatri ...
- Linux wait函数详解
wait和waitpid出现的原因 SIGCHLD --当子进程退出的时候,内核会向父进程SIGCHLD信号,子进程的退出是个异步事件(子进程可以在父进程运行的任何时刻终止) --子进程退出时,内核将 ...
- QT 常用控件一
QWidget 创建窗口 如果widget未使用腹肌进行创建,则在显示时视为窗口或顶层widget. 由于顶层widget没有父级对象类来确保在其不再使用时删除,所以需要开发人员在应用程序中对其进程跟 ...
- jquery常用代码
转自:未找到 以下是jquery中比较常用的一些操作实现方式: $("标签名") //取html元素 document.getElementsByTagName("&qu ...