C#微信开发之旅(二):基础类之HttpClientHelper(更新:SSL安全策略)

包含通过HttpClient发起get或post请求的方法,所有调用微信接口的操作都通过此类。话不多说,直接上代码:

2014-10-31代码更新:微信SSL安全策略调整,关闭掉SSLv2、SSLv3版本支持,不再支持部分使用SSLv2、 SSLv3或更低版本的客户端调用。

  1     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     }

 

posted @ 2014-10-16 17:39  Nuclear  阅读(7221)  评论(2编辑  收藏  举报

我只存在于我的世界!