C# 위챗 개발 투어(二): 기본 클래스인 HttpClientHelper(업데이트: SSL 보안 정책)

13593 단어
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     }

좋은 웹페이지 즐겨찾기