Пример #1
0
 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);
         }
     }
 }
Пример #2
0
 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> >());
     }
 }
Пример #3
0
        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);
        }
Пример #4
0
        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>());
        }