标签:win sync .text 信息 request async web ada type
using System; using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Net.Http.Headers; using System.Text; namespace Qka.UsersApi.Test { public static class HttpClientUtil { /// <summary> /// 获取要提交的信息 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="model"></param> /// <returns></returns> public static MultipartFormDataContent GetPostModelData<T>(T model) { var modelType = typeof(T); var formData = new MultipartFormDataContent(); //遍历SendData的所有成员 foreach (var item in modelType.GetProperties()) { if (item.GetValue(model) == null) { continue; } var content = new StringContent(((string)item.GetValue(model).ToString())); formData.Add(content, item.Name); } return formData; } public static string PostData<T>(this HttpClient client, string url, T model) { client.MaxResponseContentBufferSize = 256000; client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36"); var postModelData = HttpClientUtil.GetPostModelData<T>(model); var response = client.PostAsync(url, postModelData).Result; return response.Content.ReadAsStringAsync().Result; } public static string GetData(this HttpClient client, string url, Dictionary<string, string> formData=null) { client.MaxResponseContentBufferSize = 256000; client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36"); if (formData == null || formData.Count <= 0) { return client.GetAsync(url).Result.Content.ReadAsStringAsync().Result; } var paraData = formData.GetQueryString(); var questionmark = "?" + paraData; var response = client.GetAsync(url + questionmark ).Result; var restr = response.Content.ReadAsStringAsync().Result; if(string.IsNullOrEmpty(restr)) { var eeee = 11111; } return restr; } public static string PutData<T>(this HttpClient client, string url, T model) { client.MaxResponseContentBufferSize = 256000; client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36"); var postModelData = HttpClientUtil.GetPostModelData<T>(model); var response = client.PutAsync(url, postModelData).Result; return response.Content.ReadAsStringAsync().Result; } public static string DeleteData(this HttpClient client, string url, Dictionary<string, string> formData) { client.MaxResponseContentBufferSize = 256000; client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36"); if (formData == null || formData.Count <= 0) { return client.GetAsync(url).Result.Content.ReadAsStringAsync().Result; } var paraData = formData.GetQueryString(); var response = client.DeleteAsync(url + "?" + paraData).Result; return response.Content.ReadAsStringAsync().Result; } /// <summary> /// 组装QueryString的方法 /// 参数之间用&连接,首位没有符号,如:a=1&b=2&c=3 /// </summary> /// <param name="formData"></param> /// <returns></returns>public static string GetQueryString(this Dictionary<string, string> formData) { if (formData == null || formData.Count == 0) { return ""; } StringBuilder sb = new StringBuilder(); var i = 0; foreach (var kv in formData) { i++; sb.AppendFormat("{0}={1}", kv.Key, kv.Value); if (i < formData.Count) { sb.Append("&"); } } return sb.ToString(); } } }
标签:win sync .text 信息 request async web ada type
原文地址:https://www.cnblogs.com/qiywtc/p/9057451.html