包含通过HttpClient发起get或post请求的方法,所有调用微信接口的操作都通过此类。话不多说,直接上代码:
2014-10-31代码更新:微信SSL安全策略调整,关闭掉SSLv2、SSLv3版本支持,不再支持部分使用SSLv2、 SSLv3或更低版本的客户端调用。
public class HttpClientHelper
2 {
3 ///
4 /// get请求
5 ///
6 ///
7 ///
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(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 t = response.Content.ReadAsStringAsync();
42 string s = t.Result;
43
44 result = JsonConvert.DeserializeObject(s);
45 }
46 return result;
47 }
48
49 ///
50 /// post请求
51 ///
52 ///
53 /// post数据
54 ///
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 ///
75 /// 发起post请求
76 ///
77 ///
78 /// url
79 /// post数据
80 ///
81 public static T PostResponse(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 t = response.Content.ReadAsStringAsync();
98 string s = t.Result;
99
100 result = JsonConvert.DeserializeObject(s);
101 }
102 return result;
103 }
104
105 ///
106 /// V3接口全部为Xml形式,故有此方法
107 ///
108 ///
109 ///
110 ///
111 ///
112 public static T PostXmlResponse(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 t = response.Content.ReadAsStringAsync();
129 string s = t.Result;
130
131 result = XmlDeserialize(s);
132 }
133 return result;
134 }
135
136 ///
137 /// 反序列化Xml
138 ///
139 ///
140 ///
141 ///
142 public static T XmlDeserialize(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 }




