public Task <HttpResponseItem> GetResponseAsync(HttpRequestItem requestItem, CancellationToken token)
        {
            var responseItem = new HttpResponseItem()
            {
                ResultType = requestItem.ResultType
            };

            try
            {
                var httpRequest = GetHttpRequest(requestItem);
                var r           = _httpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, token);
                r.Wait();
                var result = r.Result;// await _httpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, token).ConfigureAwait(false);
                responseItem.StatusCode = result.StatusCode;
                if (!result.IsSuccessStatusCode)
                {
                    throw new WebException($"Unexpected Status Code: {result.StatusCode}");
                }

                var res = GetHttpResponseItem(result, requestItem.ResultType);
                res.Wait();
                responseItem = res.Result;// await GetHttpResponseItem(result, requestItem.ResultType).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                responseItem.Exception = ex;
            }

            return(Task.Factory.StartNew(() => { return responseItem; }));
            //return responseItem;
        }
        private static Task <HttpResponseItem> GetHttpResponseItem(HttpResponseMessage response, ResponseResultType resultType)
        {
            var responseItem = new HttpResponseItem
            {
                ResultType = resultType,
                Location   = response.Headers.Location,
                StatusCode = response.StatusCode
            };

            switch (resultType)
            {
            case ResponseResultType.String:
            {
                var r = response.Content.ReadAsStringAsync();
                r.Wait();
                responseItem.ResponseString = r.Result;        // await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                break;
            }

            case ResponseResultType.Byte:
            {
                var r = response.Content.ReadAsByteArrayAsync();
                r.Wait();
                responseItem.ResponseBytes = r.Result;        // await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
                break;
            }

            case ResponseResultType.Stream:
            {
                var r = response.Content.ReadAsByteArrayAsync();
                r.Wait();
                responseItem.ResponseStream = new MemoryStream(r.Result);        //await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false)
                break;
            }
            }
            foreach (var header in response.Headers)
            {
                responseItem.Headers[header.Key] = header.Value.ToList();
            }
            foreach (var header in response.Content.Headers)
            {
                responseItem.Headers[header.Key] = header.Value.ToList();
            }
            return(Task.Factory.StartNew(() => { return responseItem; }));
            //return responseItem;
        }
Exemplo n.º 3
0
 private static async Task<HttpResponseItem> GetHttpResponseItem(HttpResponseMessage response, ResponseResultType resultType)
 {
     var responseItem = new HttpResponseItem
     {
         ResultType = resultType,
         Location = response.Headers.Location,
         StatusCode = response.StatusCode
     };
     switch (resultType)
     {
         case ResponseResultType.String:
         {
             responseItem.ResponseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
             break;
         }
         case ResponseResultType.Byte:
         {
             responseItem.ResponseBytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
             break;
         }
         case ResponseResultType.Stream:
         {
             responseItem.ResponseStream = new MemoryStream(await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false));
             break;
         }
     }
     foreach (var header in response.Headers)
     {
         responseItem.Headers[header.Key] = header.Value.ToList();
     }
     foreach (var header in response.Content.Headers)
     {
         responseItem.Headers[header.Key] = header.Value.ToList();
     }
     return responseItem;
 }
Exemplo n.º 4
0
 public async Task<HttpResponseItem> GetResponseAsync(HttpRequestItem requestItem, CancellationToken token)
 {
     var responseItem = new HttpResponseItem() { ResultType = requestItem.ResultType };
     try
     {
         var httpRequest = GetHttpRequest(requestItem);
         var result = await _httpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, token).ConfigureAwait(false);
         responseItem.StatusCode = result.StatusCode;
         if (!result.IsSuccessStatusCode) throw new WebException($"Unexpected Status Code: {result.StatusCode}");
         responseItem = await GetHttpResponseItem(result, requestItem.ResultType).ConfigureAwait(false);
     }
     catch (Exception ex)
     {
         responseItem.Exception = ex;
     }
     return responseItem;
 }