private long GetExpectedRequestLength(IHeaderCollection incomingHeaders) { if (incomingHeaders == null) { return(0); } var headersDict = incomingHeaders.ToDictionary(x => x.Name); if (!headersDict.ContainsKey("Content-Length")) { return(0); } var headerValue = headersDict["Content-Length"]; if (headerValue == null) { return(0); } long contentLength; if (!long.TryParse(headerValue.HeaderValue, NumberStyles.Any, CultureInfo.InvariantCulture, out contentLength)) { return(0); } return(contentLength); }
public async Task <IResponseResult <TResult> > ExecuteRequestAsync <TResult>( HttpMethod method, string url, IHeaderCollection headers = null, IRequestBody body = null) { var restSharpMethod = method.ConvertToRestSharpMethod(); var request = new RestRequest(url, restSharpMethod) { RequestFormat = body.ConvertToRestSharpDataFormat() }; if (body != null) { if (body.Type == BodyTypes.Xml) { request.AddXmlBody(body.Payload); } else { request.AddJsonBody(body.Payload); } } if (headers != null) { foreach (var header in headers.ToDictionary()) { request.AddHeader(header.Key, header.Value?.ToString() ?? ""); } } var response = await this.Client.ExecuteAsync <TResult>(request); if (response.IsSuccessful) { return(new ResponseResult <TResult>(response.StatusCode) { Json = response.Content, RawData = response.RawBytes, Data = response.Data }); } else { return(new ResponseResult <TResult>(response.StatusCode) { Message = response.Content, Json = response.Content, RawData = response.RawBytes }); } }
public IHeaderCollection Add(IHeaderCollection collection) { IHeaderCollection headers = this; var dictionary = collection.ToDictionary(); foreach (var pair in dictionary) { headers = this.Add(pair.Key, pair.Value); } return(headers); }
public async Task <IResponseResult <TResult> > ExecuteRequestAsync <TResult>( HttpMethod method, string url, IHeaderCollection headers = null, IRequestBody body = null) { using (var httpClient = new HttpClient()) { var request = new HttpRequestMessage(method, url); if (headers != null) { foreach (var(key, value) in headers.ToDictionary()) { request.Headers.Add(key, value.ToString()); } } var httpContent = body?.GetHttpContent(); if (httpContent != null) { request.Content = httpContent; } var response = await httpClient.SendAsync(request); var rawData = await response.Content.ReadAsByteArrayAsync(); var json = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { return(new ResponseResult <TResult>(response.StatusCode) { Json = json, RawData = rawData, Data = Newtonsoft.Json.JsonConvert.DeserializeObject <TResult>(json), }); } else { return(new ResponseResult <TResult>(response.StatusCode, json) { Json = json, RawData = rawData }); } } }
public static IHeaderCollection Add(IHeaderCollection collection) { return(new RequestHeaders(collection.ToDictionary(x => x.Key, y => y.Value))); }
private long GetExpectedRequestLength(IHeaderCollection incomingHeaders) { if (incomingHeaders == null) { return 0; } var headersDict = incomingHeaders.ToDictionary(x => x.Name); if (!headersDict.ContainsKey("Content-Length")) { return 0; } var headerValue = headersDict["Content-Length"]; if (headerValue == null) { return 0; } long contentLength; if (!long.TryParse(headerValue.HeaderValue, NumberStyles.Any, CultureInfo.InvariantCulture, out contentLength)) { return 0; } return contentLength; }
public static IHeaderCollection Add(IHeaderCollection collection) { return(new RequestHeaders(collection.ToDictionary())); }