using System; using System.Threading.Tasks; using System.Collections.Generic; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Linq; using LC.Newtonsoft.Json; using TapTap.Common; using TapTap.Common.Internal.Http; namespace TapTap.TapDB.Standalone.Internal { public class HttpClient { private readonly string serverUrl; private readonly string apiVersion; private readonly System.Net.Http.HttpClient client; public HttpClient(string serverUrl, string apiVersion) { client = new System.Net.Http.HttpClient(new MessageHandler()); client.Timeout = TimeSpan.FromSeconds(5); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); this.serverUrl = serverUrl; this.apiVersion = apiVersion; } public Task Get(string path, Dictionary headers = null, Dictionary queryParams = null) { return Request(path, HttpMethod.Get, headers, null, queryParams); } public Task Post(string path, Dictionary headers = null, object data = null, Dictionary queryParams = null) { return Request(path, HttpMethod.Post, headers, data, queryParams); } public Task Put(string path, Dictionary headers = null, object data = null, Dictionary queryParams = null) { return Request(path, HttpMethod.Put, headers, data, queryParams); } public Task Delete(string path, Dictionary headers = null, object data = null, Dictionary queryParams = null) { return Request>(path, HttpMethod.Delete, headers, data, queryParams); } async Task Request(string path, HttpMethod method, Dictionary headers = null, object data = null, Dictionary queryParams = null) { string url = BuildUrl(path, queryParams); HttpRequestMessage request = new HttpRequestMessage { RequestUri = new Uri(url), Method = method, }; FillHeaders(request.Headers, headers); string content = null; if (data != null) { content = JsonConvert.SerializeObject(data); StringContent requestContent = new StringContent(content); requestContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); request.Content = requestContent; } TapHttpUtils.PrintRequest(client, request, content); HttpResponseMessage response; try { response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); } catch (Exception e) { TapDBStandalone.Logger.Error(e.Message); throw; } finally { request.Dispose(); } string resultString = await response.Content.ReadAsStringAsync(); response.Dispose(); TapHttpUtils.PrintResponse(response, resultString); if (response.IsSuccessStatusCode) { T ret = JsonConvert.DeserializeObject(resultString); return ret; } throw HandleErrorResponse(response.StatusCode, resultString); } Exception HandleErrorResponse(HttpStatusCode statusCode, string responseContent) { int code = (int)statusCode; string message = responseContent; // TODO 解析错误 return new TapException(code, message); } string BuildUrl(string path, Dictionary queryParams) { string url = $"{serverUrl}/{apiVersion}/{path}"; if (queryParams != null) { IEnumerable queryPairs = queryParams.Select(kv => $"{kv.Key}={Uri.EscapeDataString(kv.Value.ToString())}"); string queries = string.Join("&", queryPairs); url = $"{url}?{queries}"; } return url; } void FillHeaders(HttpRequestHeaders headers, Dictionary reqHeaders = null) { // 额外 headers if (reqHeaders != null) { foreach (KeyValuePair kv in reqHeaders) { headers.Add(kv.Key, kv.Value.ToString()); } } } } }