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#微信开发小白成长教程二(新手接入指南,附视频)
距离第一讲又已经过去了一个多星期了,本打算一周更新一讲的,奈何实在太忙.最近也在群里发现有一部分人已经可以熟练调用微信的部分接口但却不是很清楚微信公众平台接收消息的一个处理机制.本讲就来介绍下怎么接入 ...
随机推荐
- maven学习(一)
Maven这个词可以翻译为:知识的积累,也可以翻译为“专家”,“内行”.maven主要服务于鲫鱼java平台的项目构建.依赖管理和项目信息管理.无论是小型的开源类库项目,还是大型的企业级应用,无乱是传 ...
- Linux打包压缩.md
Linux下打包压缩命令 下面学习一下压缩和打包的相关命令,首先得先明确两个概念,即:压缩和打包 .我们实际使用中一般是打包和压缩结合的使用,为了学习下面简要的介绍一下压缩文件或目录的命令. 压缩:将 ...
- 夯实基础之php学习-1基础篇
1,单引号和双引号的区别 单引号表示字符串,双引号能解析字符串中的变量,所以,如果没有变量,尽量用单引号,加快解析速度 当字符串需要单引号或者双引号的时候,可以用转义字符代替 2,类型转换 通过(bo ...
- 前端见微知著工具篇:Grunt实现自动化
转载说明 本篇文章为转载文章,来源为[前端福利]用grunt搭建自动化的web前端开发环境-完整教程,之所以转载,是因为本文写的太详细了,我很想自己来写,但是发现跳不出这篇文章的圈子,因为写的详尽,所 ...
- 发布了Android的App,我要开源几个组件!
做了一款App,本来是毕业设计但是毕业的时候还没有做完,因为大部分时间都改论文去了,你们都懂的.现在毕业了在工作之余把App基本上做完了.为什么说基本上呢,因为我觉得还有很多功能还没实现,还要很多bu ...
- TF400916错误修复办法
在使用TFS作为研发过程管理工具的时候,如果调整了工作项的状态信息,可能会出现下面的错误: 要解决此问题非常简单: 1.找一台安装了VS2015程序的环境.因为我们使用的是TFS2015,所以需要对应 ...
- Qt学习笔记 信号和槽
槽和普通c++成员函数一样只可以为虚函数,也可以被重用,可以是公有的也可以是私有的,也可以被其它的c++函数调用; 参数也是任意的 唯一不同的是本槽和信号是可以连在一起的,和c#的事件差不多.相连后每 ...
- 你应该知道的25道Javascript面试题
题目来自 25 Essential JavaScript Interview Questions.闲来无事,正好切一下. 一 What is a potential pitfall with usin ...
- DOM之表格与表单基础分享
我是沐晴,好久不见.马上要放假啦,也是比较的忙. 今天来谈谈表格和表单的基本知识.前期的写的都是比较基础的知识,后期会慢慢增加实例.一起来学习吧. 先看表格,DOM中提供了一些属性,便于我们获取表单节 ...
- 一次莽撞的行为:在phpmyadmin中修改MySQL root密码后无法操作数据库
一.手贱行为(✿◡‿◡) 在一次开发中通过xampp方式安装了PHP环境,需要操作数据库时通过phpmyadmin访问MySQL,在一次数据库操作时想起没有设置密码,于是直接在mysql数据库中的us ...