包含通过HttpClient发起get或post请求的方法,所有调用微信接口的操作都通过此类。话不多说,直接上代码:
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 HttpClient httpClient = new HttpClient(); 11 httpClient.DefaultRequestHeaders.Accept.Add( 12 new MediaTypeWithQualityHeaderValue("application/json")); 13 HttpResponseMessage response = httpClient.GetAsync(url).Result; 14 15 if (response.IsSuccessStatusCode) 16 { 17 string result = response.Content.ReadAsStringAsync().Result; 18 return result; 19 } 20 return null; 21 } 22 23 public static T GetResponse<T>(string url) 24 where T : class,new() 25 { 26 HttpClient httpClient = new HttpClient(); 27 httpClient.DefaultRequestHeaders.Accept.Add( 28 new MediaTypeWithQualityHeaderValue("application/json")); 29 HttpResponseMessage response = httpClient.GetAsync(url).Result; 30 31 T result = default(T); 32 33 if (response.IsSuccessStatusCode) 34 { 35 Task<string> t = response.Content.ReadAsStringAsync(); 36 string s = t.Result; 37 38 result = JsonConvert.DeserializeObject<T>(s); 39 } 40 return result; 41 } 42 43 /// <summary> 44 /// post请求 45 /// </summary> 46 /// <param name="url"></param> 47 /// <param name="postData">post数据</param> 48 /// <returns></returns> 49 public static string PostResponse(string url, string postData) 50 { 51 HttpContent httpContent = new StringContent(postData); 52 httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); 53 HttpClient httpClient = new HttpClient(); 54 55 HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result; 56 57 if (response.IsSuccessStatusCode) 58 { 59 string result = response.Content.ReadAsStringAsync().Result; 60 return result; 61 } 62 return null; 63 } 64 65 /// <summary> 66 /// 发起post请求 67 /// </summary> 68 /// <typeparam name="T"></typeparam> 69 /// <param name="url">url</param> 70 /// <param name="postData">post数据</param> 71 /// <returns></returns> 72 public static T PostResponse<T>(string url, string postData) 73 where T : class,new() 74 { 75 HttpContent httpContent = new StringContent(postData); 76 httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); 77 HttpClient httpClient = new HttpClient(); 78 79 T result = default(T); 80 81 HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result; 82 83 if (response.IsSuccessStatusCode) 84 { 85 Task<string> t = response.Content.ReadAsStringAsync(); 86 string s = t.Result; 87 88 result = JsonConvert.DeserializeObject<T>(s); 89 } 90 return result; 91 } 92 93 /// <summary> 94 /// V3接口全部为Xml形式,故有此方法 95 /// </summary> 96 /// <typeparam name="T"></typeparam> 97 /// <param name="url"></param> 98 /// <param name="xmlString"></param> 99 /// <returns></returns> 100 public static T PostXmlResponse<T>(string url, string xmlString) 101 where T : class,new() 102 { 103 HttpContent httpContent = new StringContent(xmlString); 104 httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); 105 HttpClient httpClient = new HttpClient(); 106 107 T result = default(T); 108 109 HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result; 110 111 if (response.IsSuccessStatusCode) 112 { 113 Task<string> t = response.Content.ReadAsStringAsync(); 114 string s = t.Result; 115 116 result = XmlDeserialize<T>(s); 117 } 118 return result; 119 } 120 121 /// <summary> 122 /// 反序列化Xml 123 /// </summary> 124 /// <typeparam name="T"></typeparam> 125 /// <param name="xmlString"></param> 126 /// <returns></returns> 127 public static T XmlDeserialize<T>(string xmlString) 128 where T : class,new () 129 { 130 try 131 { 132 XmlSerializer ser = new XmlSerializer(typeof(T)); 133 using (StringReader reader = new StringReader(xmlString)) 134 { 135 return (T)ser.Deserialize(reader); 136 } 137 } 138 catch (Exception ex) 139 { 140 throw new Exception("XmlDeserialize发生异常:xmlString:" + xmlString + "异常信息:" + ex.Message); 141 } 142 143 } 144 }