public static string Send(HttpParam param, IHttpException iHttpException = null, HttpClient httpClient = null, CancellationToken cancellationToken = default(CancellationToken)) { using (var client = httpClient ?? new HttpClient()) { HttpRequestMessage request = null; HttpResponseMessage response = null; request = GetHttpRequestMessage(param); response = client.SendAsync(request, cancellationToken).Result; if (response.IsSuccessStatusCode) { return(response.Content.ReadAsStringAsync().Result); } else { var message = $"send api {param.Uri.ToString()} error"; var exception = new HttpException(message, param.Uri.ToString(), param.Method, response); if (iHttpException == null) { throw exception; } iHttpException.SetHttpException(exception); return(null); } } }
public static IEnumerable <TranslateBaiDuProxyResult> Translate(TranslateBaiDuProxyParam param) { using (var client = new HttpClient()) { var httpParam = new HttpParam() { IsUseBase = false, Method = HttpMethod.GET, Content = null, Url = GetProxyUrl(param) }; var result = HttpRequest.Send(httpParam, httpClient: client).DeserializeObject <JObject>(); return(result["trans_result"]?.ToObject <IEnumerable <TranslateBaiDuProxyResult> >()); } }
private static HttpRequestMessage GetHttpRequestMessage(HttpParam param) { var request = new HttpRequestMessage(); if (param.Content != null) { request.Content = param.Content; if (!string.IsNullOrEmpty(param.Type)) { request.Content.Headers.ContentType = new MediaTypeWithQualityHeaderValue(param.Type); } } request.RequestUri = param.Uri; request.Method = new System.Net.Http.HttpMethod(param.Method.ToString()); return(request); }
public static T Send <T>(HttpParam param, IHttpException iHttpException = null, HttpClient httpClient = null, CancellationToken cancellationToken = default(CancellationToken)) { var value = Send(param, iHttpException, httpClient, cancellationToken); return(value.DeserializeObject <T>()); }