1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| public class OdinHttpClientFactory : IOdinHttpClientFactory { public static async Task<T> GetRequestAsync<T>(string clientName, string uri, Dictionary<string, string> customHeaders = null, string mediaType = "application/json") { var clientFactory = OdinInjectHelper.GetService<IHttpClientFactory>(); var client = clientFactory.CreateClient(clientName); var request = new HttpRequestMessage() { RequestUri = new Uri(uri), Method = HttpMethod.Get, }; RequestHeaderAdd(request, customHeaders); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(mediaType)); return await GetResponseResult<T>(client, request); }
public static async Task<T> PostRequestAsync<T>(string clientName, string uri, Object obj, Dictionary<string, string> customHeaders = null, string mediaType = "application/json", Encoding encoder = null) { var clientFactory = OdinInjectHelper.GetService<IHttpClientFactory>(); var client = clientFactory.CreateClient(clientName); var request = new HttpRequestMessage() { RequestUri = new Uri(uri), Method = HttpMethod.Post, }; RequestHeaderAdd(request, customHeaders); request.Content = GenerateContent(obj, mediaType, encoder); return await GetResponseResult<T>(client, request); }
private static HttpContent GenerateContent(Object obj, string mediaType, Encoding encoder) { if (typeof(String) == obj.GetType()) { return GenerateContent<String>(obj.ToString(), mediaType, encoder); } else { return GenerateContent<Object>(obj, mediaType, encoder); } } private static HttpContent GenerateContent<T>(T obj, string mediaType, Encoding encoder) { StringBuilder jsonContent = new StringBuilder(); string sendContent = string.Empty; Dictionary<string, string> dic = ConvertPostDataToDictionary<T>(obj, encoder); if (mediaType == "application/json") { sendContent = JsonConvert.SerializeObject(dic); } else { sendContent = ConvertDictionaryToPostFormData(dic).ToString(); } return new StringContent( sendContent, encoder == null ? Encoding.UTF8 : encoder, mediaType); }
private async static Task<T> PostResponseResult<T>(HttpClient client, HttpRequestMessage request) { var response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { return GetResult<T>(response); } else throw new Exception("请求出错"); } private async static Task<T> GetResponseResult<T>(HttpClient client, HttpRequestMessage request) { var response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { return GetResult<T>(response); } else throw new Exception("请求出错"); } private static void RequestHeaderAdd(HttpRequestMessage request, Dictionary<string, string> customHeaders) { if (customHeaders != null) { foreach (KeyValuePair<string, string> customHeader in customHeaders) { request.Headers.Add(customHeader.Key, customHeader.Value); } } } private static T GetResult<T>(HttpResponseMessage httpResponseMessage) { if (typeof(T) == typeof(byte[])) { return (T)Convert.ChangeType(httpResponseMessage.Content.ReadAsByteArrayAsync(), typeof(T)); } else if (typeof(T) == typeof(Stream)) { return (T)Convert.ChangeType(httpResponseMessage.Content.ReadAsStreamAsync().Result, typeof(T)); } else { if (typeof(T) == typeof(string)) return (T)Convert.ChangeType(httpResponseMessage.Content.ReadAsStringAsync().Result, typeof(T)); return JsonConvert.DeserializeObject<T>(httpResponseMessage.Content.ReadAsStringAsync().Result); } }
public static Dictionary<string, string> ConvertPostDataToDictionary<T>(T obj, Encoding encoder = null) { Dictionary<string, string> dic = new Dictionary<string, string>(); if (typeof(T) == typeof(String)) { foreach (var item in obj.ToString().Split('&')) { dic.Add( item.Split('=')[0], encoder == null || encoder == Encoding.UTF8 ? item.Split('=')[1] : item.Split('=')[1].ConvertStringEncode(Encoding.UTF8, encoder) ); } } else { foreach (var item in obj.GetType().GetRuntimeProperties()) { dic.Add(item.Name, encoder == null || encoder == Encoding.UTF8 ? item.GetValue(obj).ToString() : item.GetValue(obj).ToString().ConvertStringEncode(Encoding.UTF8, encoder) ); } } return dic; } private static StringBuilder ConvertDictionaryToPostFormData(Dictionary<string, string> dic) { StringBuilder builder = new StringBuilder(); if (dic != null) { bool hasParam = false; foreach (KeyValuePair<string, string> kv in dic) { string name = kv.Key; string value = kv.Value; if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value)) { if (hasParam) { builder.Append("&"); } builder.Append(name); builder.Append("="); builder.Append(value); hasParam = true; } } } return builder; } }
|