WebApi帮助类
public class HttpClientBase { /// <summary> /// HttpClient实现Post请求 /// </summary> protected async Task<object> dooPost(string url, Dictionary<string, string> dic) { //设置HttpClientHandler的AutomaticDecompression var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; //创建HttpClient(注意传入HttpClientHandler) using (var http = new HttpClient(handler)) { //使用FormUrlEncodedContent做HttpContent var content = new FormUrlEncodedContent(dic); //await异步等待回应 var response = await http.PostAsync(url, content); //确保HTTP成功状态值 response.EnsureSuccessStatusCode(); //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip) string Result = await response.Content.ReadAsStringAsync(); var Item = JsonConvert.DeserializeObject(Result); return Item; } } /// <summary> /// HttpClient实现Get请求 /// </summary> protected async Task<object> dooGet(string url) { //创建HttpClient(注意传入HttpClientHandler) var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; string Result = ""; using (var http = new HttpClient(handler)) { //await异步等待回应 var response = await http.GetAsync(url); //确保HTTP成功状态值 response.EnsureSuccessStatusCode(); //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip) Result = await response.Content.ReadAsStringAsync(); var Items= JsonConvert.DeserializeObject(Result); return Items; } } /// <summary> /// HttpClient实现Put请求 /// </summary> protected async Task<object> dooPut(string url, Dictionary<string, string> dic) { //设置HttpClientHandler的AutomaticDecompression var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; string Result=""; //创建HttpClient(注意传入HttpClientHandler) using (var http = new HttpClient(handler)) { //使用FormUrlEncodedContent做HttpContent var content = new FormUrlEncodedContent(dic); //await异步等待回应 var response = await http.PutAsync(url, content); //确保HTTP成功状态值 response.EnsureSuccessStatusCode(); //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip) JavaScriptSerializer Serializer = new JavaScriptSerializer(); var items = Serializer.DeserializeObject(Result); return items; } } /// <summary> /// HttpClient实现Get请求 /// </summary> /// <param name="url"></param> /// <returns></returns> protected object doGet(string url) { HttpClient client = new HttpClient(); client.BaseAddress = new Uri(url); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.GetAsync(url).Result; // Blocking call(阻塞调用)! var result = ""; if (response.IsSuccessStatusCode) { result = response.Content.ReadAsStringAsync().Result; JavaScriptSerializer Serializer = new JavaScriptSerializer(); var items = Serializer.DeserializeObject(result); return items; } return result; } /// <summary> /// HttpClient实现Post请求 /// </summary> /// <param name="url"></param> /// <returns></returns> protected object doPost(string url, Dictionary<string, string> dic,ref string StatusCode) { HttpClient client = new HttpClient(); client.BaseAddress = new Uri(url); JavaScriptSerializer jss = new JavaScriptSerializer(); var content = new FormUrlEncodedContent(dic); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.PostAsync(url, content).Result; var result = ""; if (response.IsSuccessStatusCode) { result = response.Content.ReadAsStringAsync().Result; JavaScriptSerializer Serializer = new JavaScriptSerializer(); var items = Serializer.DeserializeObject(result); //Dictionary<string, object> jarry = (Dictionary<string, object>)items; //obj ss = jarry["IsSuccess"]; //string sate = jarry[0]["IsSuccess"].ToString(); return items; }else { StatusCode = response.StatusCode.ToString(); ; } return result; } /// <summary> /// 转换URL /// </summary> /// <param name="str"></param> /// <returns></returns> public static string UrlEncode(string str) { StringBuilder sb = new StringBuilder(); byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str); for (int i = 0; i < byStr.Length; i++) { sb.Append(@"%" + Convert.ToString(byStr[i], 16)); } return (sb.ToString()); } }