C# ASP.NET Core使用HttpClient的同步和异步请求
引用 Newtonsoft.Json
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// Post请求 public string PostResponse(string url,string postData,out string statusCode) { string result = string.Empty; //设置Http的正文 HttpContent httpContent = new StringContent(postData); //设置Http的内容标头 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); //设置Http的内容标头的字符 httpContent.Headers.ContentType.CharSet = "utf-8"; using(HttpClient httpClient=new HttpClient()) { //异步Post HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result; //输出Http响应状态码 statusCode = response.StatusCode.ToString(); //确保Http响应成功 if (response.IsSuccessStatusCode) { //异步读取json result = response.Content.ReadAsStringAsync().Result; } } return result; } // 泛型:Post请求 public T PostResponse<T>(string url,string postData) where T:class,new() { T result = default(T); HttpContent httpContent = new StringContent(postData); httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); httpContent.Headers.ContentType.CharSet = "utf-8"; using(HttpClient httpClient=new HttpClient()) { HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result; if (response.IsSuccessStatusCode) { Task<string> t = response.Content.ReadAsStringAsync(); string s = t.Result; //Newtonsoft.Json string json = JsonConvert.DeserializeObject(s).ToString(); result = JsonConvert.DeserializeObject<T>(json); } } return result; }
参数为键值对:var content = new FormUrlEncodedContent(postData);
使用HttpClient 请求的时候碰到个问题不知道是什么异常,用HttpWebResponse请求才获取到异常,设置ServicePointManager可以用,参考下面这个
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary> /// http请求接口 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="url"></param> /// <param name="postData"></param> /// <returns></returns> public T httpClientPost<T>(string url, string postData) where T : class, new() { T result = default(T); ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; HttpContent httpContent = new StringContent(postData); httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); httpContent.Headers.ContentType.CharSet = "utf-8"; using (HttpClient httpClient = new HttpClient()) { HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result; if (response.IsSuccessStatusCode) { Task<string> t = response.Content.ReadAsStringAsync(); string s = t.Result; //Newtonsoft.Json string json = JsonConvert.DeserializeObject(s).ToString(); result = JsonConvert.DeserializeObject<T>(json); } } return result; } public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { // 总是接受 return true; } /// <summary> /// http请求接口 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="url"></param> /// <param name="bodyString"></param> /// <returns></returns> public VMDataResponse<T> HttpPost<T>(string url, string bodyString) { var result = new VMDataResponse<T>(); try { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); var request = WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/json; charset=utf-8"; byte[] buffer = Encoding.UTF8.GetBytes(bodyString); request.ContentLength = buffer.Length; System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; request.GetRequestStream().Write(buffer, 0, buffer.Length); var response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode != HttpStatusCode.OK) { result.msg = "错误:Respose返回不是StatusCode!=OK"; result.status = 500; logger.Error(result.msg); return result; } using (var ss = response.GetResponseStream()) { byte[] rbs = new byte[4096]; int count = 0; string str = ""; while (ss != null && (count = ss.Read(rbs, 0, rbs.Length)) > 0) { str += Encoding.UTF8.GetString(rbs, 0, count); } var msg = str; //logger.Error("返回信息" + msg); if (!string.IsNullOrWhiteSpace(msg)) { result.data = JsonConvert.DeserializeObject<T>(msg); return result; } } } catch (Exception ex) { result.msg = "错误:请求出现异常消息:" + ex.Message; result.status = 600; logger.Error(result.msg); return result; } return result; }
get:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// 泛型:Get请求 public T GetResponse<T>(string url) where T :class,new() { T result = default(T); using (HttpClient httpClient=new HttpClient()) { httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = httpClient.GetAsync(url).Result; if (response.IsSuccessStatusCode) { Task<string> t = response.Content.ReadAsStringAsync(); string s = t.Result; string json = JsonConvert.DeserializeObject(s).ToString(); result = JsonConvert.DeserializeObject<T>(json); } } return result; } // Get请求 public string GetResponse(string url, out string statusCode) { string result = string.Empty; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = httpClient.GetAsync(url).Result; statusCode = response.StatusCode.ToString(); if (response.IsSuccessStatusCode) { result = response.Content.ReadAsStringAsync().Result; } } return result; }
put:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// Put请求 public string PutResponse(string url, string putData, out string statusCode) { string result = string.Empty; HttpContent httpContent = new StringContent(putData); httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); httpContent.Headers.ContentType.CharSet = "utf-8"; using (HttpClient httpClient = new HttpClient()) { HttpResponseMessage response = httpClient.PutAsync(url, httpContent).Result; statusCode = response.StatusCode.ToString(); if (response.IsSuccessStatusCode) { result = response.Content.ReadAsStringAsync().Result; } } return result; } // 泛型:Put请求 public T PutResponse<T>(string url, string putData) where T : class, new() { T result = default(T); HttpContent httpContent = new StringContent(putData); httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); httpContent.Headers.ContentType.CharSet = "utf-8"; using(HttpClient httpClient=new HttpClient()) { HttpResponseMessage response = httpClient.PutAsync(url, httpContent).Result; if (response.IsSuccessStatusCode) { Task<string> t = response.Content.ReadAsStringAsync(); string s = t.Result; string json = JsonConvert.DeserializeObject(s).ToString(); result = JsonConvert.DeserializeObject<T>(json); } } return result; }
https://blog.csdn.net/sun_zeliang/article/details/81587835
https://blog.csdn.net/baidu_32739019/article/details/78679129
ASP.NET Core使用HttpClient的同步和异步请求
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
ASP.NET Core使用HttpClient的同步和异步请求 using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Web; namespace Common { public class HttpHelper {/// <summary> /// 发起POST同步请求 /// /// </summary> /// <param name="url"></param> /// <param name="postData"></param> /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param> /// <param name="headers">填充消息头</param> /// <returns></returns> public static string HttpPost(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null) { postData = postData ?? ""; using (HttpClient client = new HttpClient()) { if (headers != null) { foreach (var header in headers) client.DefaultRequestHeaders.Add(header.Key, header.Value); } using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8)) { if (contentType != null) httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); HttpResponseMessage response = client.PostAsync(url, httpContent).Result; return response.Content.ReadAsStringAsync().Result; } } } /// <summary> /// 发起POST异步请求 /// </summary> /// <param name="url"></param> /// <param name="postData"></param> /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param> /// <param name="headers">填充消息头</param> /// <returns></returns> public static async Task<string> HttpPostAsync(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null) { postData = postData ?? ""; using (HttpClient client = new HttpClient()) { client.Timeout = new TimeSpan(0, 0, timeOut); if (headers != null) { foreach (var header in headers) client.DefaultRequestHeaders.Add(header.Key, header.Value); } using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8)) { if (contentType != null) httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); HttpResponseMessage response = await client.PostAsync(url, httpContent); return await response.Content.ReadAsStringAsync(); } } } /// <summary> /// 发起GET同步请求 /// </summary> /// <param name="url"></param> /// <param name="headers"></param> /// <param name="contentType"></param> /// <returns></returns> public static string HttpGet(string url, string contentType = null, Dictionary<string, string> headers = null) { using (HttpClient client = new HttpClient()) { if (contentType != null) client.DefaultRequestHeaders.Add("ContentType", contentType); if (headers != null) { foreach (var header in headers) client.DefaultRequestHeaders.Add(header.Key, header.Value); } HttpResponseMessage response = client.GetAsync(url).Result; return response.Content.ReadAsStringAsync().Result; } } /// <summary> /// 发起GET异步请求 /// </summary> /// <param name="url"></param> /// <param name="headers"></param> /// <param name="contentType"></param> /// <returns></returns> public static async Task<string> HttpGetAsync(string url, string contentType = null, Dictionary<string, string> headers = null) { using (HttpClient client = new HttpClient()) { if (contentType != null) client.DefaultRequestHeaders.Add("ContentType", contentType); if (headers != null) { foreach (var header in headers) client.DefaultRequestHeaders.Add(header.Key, header.Value); } HttpResponseMessage response = await client.GetAsync(url); return await response.Content.ReadAsStringAsync(); } } } }
调用异步请求方法:
var result = await HttpHelper.HttpPostAsync("http://www.baidu.com/api/getuserinfo", "userid=23456798");
https://www.cnblogs.com/pudefu/p/7581956.html
https://blog.csdn.net/slowlifes/article/details/78504782
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class HttpHelper { /// <summary> /// 发起POST同步请求 /// </summary> /// <param name="url"></param> /// <param name="postData"></param> /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param> /// <param name="headers">填充消息头</param> /// <returns></returns> public static string HttpPost(string url, string postData = null, Dictionary<string, string> headers = null, string contentType = null, int timeOut = 30) { postData = postData ?? ""; if (url.StartsWith("https")) { System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; } using (HttpClient client = new HttpClient()) { if (headers != null) { foreach (var header in headers) client.DefaultRequestHeaders.Add(header.Key, header.Value); } //FormData形式的Post请求 //var content = new MultipartFormDataContent(); //content.Add(new StringContent("张三"), "name"); //JSON FormUrl:application/x-www-form-urlencoded //var content = new StringContent(json); //HttpResponseMessage response = await httpClient.PostAsync(new Uri(url), new FormUrlEncodedContent(paramDic)); //Authorization //httpClient.DefaultRequestHeaders.Add("Authorization", @"Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJndWlkIjoibXItMjAyMS0xMC0yNS1nSlJfWXNNQmJuLTJYeTYwYjA5WUVfMGpWN3RUT1h4MmY1Y3JFQ3pPNmdpT1V1czFaQktNLUhEV1NUOEd1ZlZtTHZVY01yay1JSSIsImlhdCI6MTYzNTE0Mjg5NCwiaXNzIjoiZGF0YS5taWRsYW5kLmNvbS5oayJ9.OpYHdETIud_WWzSl1eIhLL18oUZLX9ZNYqqtCQlmV7KqH7Djckp_6StjtjR3NrRrWUuRYAHyUv_idq3IajN9TyK2YvJd1pDkfT_U-do7pchb3muSx6x1LkWhpJSO4F-x2_0AounUcGTuaSpy1DrkoHVClqmeVyo4CVOP9DOcYtBVl-di0v2N6FGtiwFdjhunO4J0530sMCTIPcQHV9MF5AjqqrCLadCsf641NkZUCiAcicuCmi2c5R6rdsMaVPOoL1dXbtFpmeLd2Os6wioHgmK_c5cB90uqh4Sk7vJKm_eKi2ErXhHshGi56pZcnJNjfe4_T04ojD7Gbaf2oDm08BOjEXr7uVH5HcJ-WE4FLpZKmQolmtFQj276mhkSeEorx15WjIhjaaiFmeUsbXcbEt9V8FdMX2ZGWPpkK3BE70mg78_pB7-VSe6-gLflPooEliKz8fMx6T362e5KhZzJNhf7FJtzROMtzWelXMbnycYg_fXeVc0mxf-W7XLexYw6xDaFQEnj1mSoEtLW1S1b41ztp49IKua5oWuEzI0EqOXFXl1h5EEiNhcMYQB7oV5tQd5h74ppZGBpw4jL9J8EyelnX-MwqXR6SEwBj3f8JQkAB4tbYTNA0Lzg35sGSUpUU9ofLzMtafEMSmOLGJy7cOsatR258cRlGSAg-nvjfCE"); using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8)) { if (contentType != null) httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); HttpResponseMessage response = client.PostAsync(url, httpContent).Result; return response.Content.ReadAsStringAsync().Result; } } } /// <summary> /// 发起POST异步请求 /// </summary> /// <param name="url"></param> /// <param name="postData"></param> /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param> /// <param name="headers">填充消息头</param> /// <returns></returns> public static async Task<string> HttpPostAsync(string url, string postData = null, Dictionary<string, string> headers = null, string contentType = null, int timeOut = 30) { postData = postData ?? ""; if (url.StartsWith("https")) { System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; } using (HttpClient client = new HttpClient()) { client.Timeout = new TimeSpan(0, 0, timeOut); if (headers != null) { foreach (var header in headers) client.DefaultRequestHeaders.Add(header.Key, header.Value); } using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8)) { if (contentType != null) httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); HttpResponseMessage response = await client.PostAsync(url, httpContent); //return await response.Content.ReadAsStringAsync(); //如果响应状态代码不指示成功,则调用 EnsureSuccessStatusCode 引发异常 response.EnsureSuccessStatusCode(); //输出Http响应状态码 var statusCode = response.StatusCode.ToString(); string result = string.Empty; //确保Http响应成功 if (response.IsSuccessStatusCode) { //异步读取json result = await response.Content.ReadAsStringAsync(); } return result; } } } /// <summary> /// 发起GET同步请求 /// </summary> /// <param name="url"></param> /// <param name="headers"></param> /// <param name="contentType"></param> /// <returns></returns> public static string HttpGet(string url, Dictionary<string, string> headers = null) { if (url.StartsWith("https")) { System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; } using (HttpClient client = new HttpClient()) { if (headers != null) { foreach (var header in headers) client.DefaultRequestHeaders.Add(header.Key, header.Value); } HttpResponseMessage response = client.GetAsync(url).Result; return response.Content.ReadAsStringAsync().Result; } } /// <summary> /// 发起GET异步请求 /// </summary> /// <param name="url"></param> /// <param name="headers"></param> /// <param name="contentType"></param> /// <returns></returns> public static async Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null) { if (url.StartsWith("https")) { System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; } using (HttpClient client = new HttpClient()) { if (headers != null) { foreach (var header in headers) client.DefaultRequestHeaders.Add(header.Key, header.Value); } HttpResponseMessage response = await client.GetAsync(url); //return await response.Content.ReadAsStringAsync(); //输出Http响应状态码 var statusCode = response.StatusCode.ToString(); string result = string.Empty; //确保Http响应成功 if (response.IsSuccessStatusCode) { //异步读取json result = await response.Content.ReadAsStringAsync(); } return result; } } #region 另外的 //// Post请求 //public string PostResponse(string url, string postData, out string statusCode) //{ // string result = string.Empty; // //设置Http的正文 // HttpContent httpContent = new StringContent(postData); // //设置Http的内容标头 // httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); // //设置Http的内容标头的字符 // httpContent.Headers.ContentType.CharSet = "utf-8"; // using (HttpClient httpClient = new HttpClient()) // { // //异步Post // HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result; // //输出Http响应状态码 // statusCode = response.StatusCode.ToString(); // //确保Http响应成功 // if (response.IsSuccessStatusCode) // { // //异步读取json // result = response.Content.ReadAsStringAsync().Result; // } // } // return result; //} //// 泛型:Post请求 //public T PostResponse<T>(string url, string postData) where T : class, new() //{ // T result = default(T); // HttpContent httpContent = new StringContent(postData); // httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); // httpContent.Headers.ContentType.CharSet = "utf-8"; // using (HttpClient httpClient = new HttpClient()) // { // HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result; // if (response.IsSuccessStatusCode) // { // Task<string> t = response.Content.ReadAsStringAsync(); // string s = t.Result; // //Newtonsoft.Json // string json = JsonConvert.DeserializeObject(s).ToString(); // result = JsonConvert.DeserializeObject<T>(json); // } // } // return result; //} ///// <summary> ///// http请求接口 ///// </summary> ///// <typeparam name="T"></typeparam> ///// <param name="url"></param> ///// <param name="postData"></param> ///// <returns></returns> //public T httpClientPost<T>(string url, string postData) where T : class, new() //{ // T result = default(T); // ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); // System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; // HttpContent httpContent = new StringContent(postData); // httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); // httpContent.Headers.ContentType.CharSet = "utf-8"; // using (HttpClient httpClient = new HttpClient()) // { // HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result; // if (response.IsSuccessStatusCode) // { // Task<string> t = response.Content.ReadAsStringAsync(); // string s = t.Result; // //Newtonsoft.Json // string json = JsonConvert.DeserializeObject(s).ToString(); // result = JsonConvert.DeserializeObject<T>(json); // } // } // return result; //} //public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) //{ // 总是接受 // return true; //} ///// <summary> ///// http请求接口 ///// </summary> ///// <typeparam name="T"></typeparam> ///// <param name="url"></param> ///// <param name="bodyString"></param> ///// <returns></returns> //public VMDataResponse<T> HttpPost<T>(string url, string bodyString) //{ // var result = new VMDataResponse<T>(); // try // { // ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); // var request = WebRequest.Create(url); // request.Method = "POST"; // request.ContentType = "application/json; charset=utf-8"; // byte[] buffer = Encoding.UTF8.GetBytes(bodyString); // request.ContentLength = buffer.Length; // System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; // request.GetRequestStream().Write(buffer, , buffer.Length); // var response = (HttpWebResponse)request.GetResponse(); // if (response.StatusCode != HttpStatusCode.OK) // { // result.msg = "错误:Respose返回不是StatusCode!=OK"; // result.status = ; // logger.Error(result.msg); // return result; // } // using (var ss = response.GetResponseStream()) // { // byte[] rbs = new byte[]; // int count = ; // string str = ""; // while (ss != null && (count = ss.Read(rbs, , rbs.Length)) > ) // { // str += Encoding.UTF8.GetString(rbs, , count); // } // var msg = str; // //logger.Error("返回信息" + msg); // if (!string.IsNullOrWhiteSpace(msg)) // { // result.data = JsonConvert.DeserializeObject<T>(msg); // return result; // } // } // } // catch (Exception ex) // { // result.msg = "错误:请求出现异常消息:" + ex.Message; // result.status = ; // logger.Error(result.msg); // return result; // } // return result; //} //// 泛型:Get请求 //public T GetResponse<T>(string url) where T : class, new() //{ // T result = default(T); // using (HttpClient httpClient = new HttpClient()) // { // httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); // HttpResponseMessage response = httpClient.GetAsync(url).Result; // if (response.IsSuccessStatusCode) // { // Task<string> t = response.Content.ReadAsStringAsync(); // string s = t.Result; // string json = JsonConvert.DeserializeObject(s).ToString(); // result = JsonConvert.DeserializeObject<T>(json); // } // } // return result; //} //// Get请求 //public string GetResponse(string url, out string statusCode) //{ // string result = string.Empty; // using (HttpClient httpClient = new HttpClient()) // { // httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); // HttpResponseMessage response = httpClient.GetAsync(url).Result; // statusCode = response.StatusCode.ToString(); // if (response.IsSuccessStatusCode) // { // result = response.Content.ReadAsStringAsync().Result; // } // } // return result; //} //// Put请求 //public string PutResponse(string url, string putData, out string statusCode) //{ // string result = string.Empty; // HttpContent httpContent = new StringContent(putData); // httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); // httpContent.Headers.ContentType.CharSet = "utf-8"; // using (HttpClient httpClient = new HttpClient()) // { // HttpResponseMessage response = httpClient.PutAsync(url, httpContent).Result; // statusCode = response.StatusCode.ToString(); // if (response.IsSuccessStatusCode) // { // result = response.Content.ReadAsStringAsync().Result; // } // } // return result; //} //// 泛型:Put请求 //public T PutResponse<T>(string url, string putData) where T : class, new() //{ // T result = default(T); // HttpContent httpContent = new StringContent(putData); // httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); // httpContent.Headers.ContentType.CharSet = "utf-8"; // using (HttpClient httpClient = new HttpClient()) // { // HttpResponseMessage response = httpClient.PutAsync(url, httpContent).Result; // if (response.IsSuccessStatusCode) // { // Task<string> t = response.Content.ReadAsStringAsync(); // string s = t.Result; // string json = JsonConvert.DeserializeObject(s).ToString(); // result = JsonConvert.DeserializeObject<T>(json); // } // } // return result; //} #endregion }