.Net Core 中使用HttpClient调用Post和Get方法
1 using ICSharpCode.SharpZipLib.GZip; 2 using Newtonsoft.Json; 3 using System; 4 using System.Collections.Generic; 5 using System.IO; 6 using System.Net; 7 using System.Net.Http; 8 using System.Net.Http.Headers; 9 using System.Threading.Tasks; 10 11 /// <summary> 12 /// 基于HttpClient封装的请求类 13 /// </summary> 14 public class HttpRequest 15 { 16 /// <summary> 17 /// 使用post方法异步请求 18 /// </summary> 19 /// <param name="url">目标链接</param> 20 /// <param name="json">发送的参数字符串,只能用json</param> 21 /// <returns>返回的字符串</returns> 22 public static async Task<string> PostAsyncJson(string url, string json) 23 { 24 HttpClient client = new HttpClient(); 25 HttpContent content = new StringContent(json); 26 content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); 27 HttpResponseMessage response = await client.PostAsync(url, content); 28 response.EnsureSuccessStatusCode(); 29 string responseBody = await response.Content.ReadAsStringAsync(); 30 return responseBody; 31 } 32 33 /// <summary> 34 /// 使用post方法异步请求 35 /// </summary> 36 /// <param name="url">目标链接</param> 37 /// <param name="data">发送的参数字符串</param> 38 /// <returns>返回的字符串</returns> 39 public static async Task<string> PostAsync(string url, string data, Dictionary<string, string> header = null, bool Gzip = false) 40 { 41 HttpClient client = new HttpClient(new HttpClientHandler() { UseCookies = false }); 42 HttpContent content = new StringContent(data); 43 if (header != null) 44 { 45 client.DefaultRequestHeaders.Clear(); 46 foreach (var item in header) 47 { 48 client.DefaultRequestHeaders.Add(item.Key, item.Value); 49 } 50 } 51 HttpResponseMessage response = await client.PostAsync(url, content); 52 response.EnsureSuccessStatusCode(); 53 string responseBody = ""; 54 if (Gzip) 55 { 56 GZipInputStream inputStream = new GZipInputStream(await response.Content.ReadAsStreamAsync()); 57 responseBody = new StreamReader(inputStream).ReadToEnd(); 58 } 59 else 60 { 61 responseBody = await response.Content.ReadAsStringAsync(); 62 63 } 64 return responseBody; 65 } 66 67 /// <summary> 68 /// 使用get方法异步请求 69 /// </summary> 70 /// <param name="url">目标链接</param> 71 /// <returns>返回的字符串</returns> 72 public static async Task<string> GetAsync(string url, Dictionary<string, string> header = null, bool Gzip = false) 73 { 74 75 HttpClient client = new HttpClient(new HttpClientHandler() { UseCookies = false }); 76 if (header != null) 77 { 78 client.DefaultRequestHeaders.Clear(); 79 foreach (var item in header) 80 { 81 client.DefaultRequestHeaders.Add(item.Key, item.Value); 82 } 83 } 84 HttpResponseMessage response = await client.GetAsync(url); 85 response.EnsureSuccessStatusCode();//用来抛异常的 86 string responseBody = ""; 87 if (Gzip) 88 { 89 GZipInputStream inputStream = new GZipInputStream(await response.Content.ReadAsStreamAsync()); 90 responseBody = new StreamReader(inputStream).ReadToEnd(); 91 } 92 else 93 { 94 responseBody = await response.Content.ReadAsStringAsync(); 95 96 } 97 return responseBody; 98 } 99 100 /// <summary> 101 /// 使用post返回异步请求直接返回对象 102 /// </summary> 103 /// <typeparam name="T">返回对象类型</typeparam> 104 /// <typeparam name="T2">请求对象类型</typeparam> 105 /// <param name="url">请求链接</param> 106 /// <param name="obj">请求对象数据</param> 107 /// <returns>请求返回的目标对象</returns> 108 public static async Task<T> PostObjectAsync<T, T2>(string url, T2 obj) 109 { 110 String json = JsonConvert.SerializeObject(obj); 111 string responseBody = await PostAsyncJson(url, json); //请求当前账户的信息 112 return JsonConvert.DeserializeObject<T>(responseBody);//把收到的字符串序列化 113 } 114 115 /// <summary> 116 /// 使用Get返回异步请求直接返回对象 117 /// </summary> 118 /// <typeparam name="T">请求对象类型</typeparam> 119 /// <param name="url">请求链接</param> 120 /// <returns>返回请求的对象</returns> 121 public static async Task<T> GetObjectAsync<T>(string url) 122 { 123 string responseBody = await GetAsync(url); //请求当前账户的信息 124 return JsonConvert.DeserializeObject<T>(responseBody);//把收到的字符串序列化 125 } 126 }