/// <summary> /// Gets response /// </summary> /// <param name="request">request information</param> /// <param name="isAsync">is async</param> /// <param name="hook">Hook before requesting</param> /// <returns>response information</returns> public async Task<HttpResponse> GetResponseAsync(HttpRequest request, bool isAsync, Action<HttpClient> hook = null) { var baseUri = new Uri(this.baseUriMapping[request.BaseUri]); var requestUri = CreateUri(baseUri, request.PathInfos, request.QueryStrings, request.Fragment); request.Uri = requestUri.ToString(); var handler = new HttpClientHandler(); if (!string.IsNullOrWhiteSpace(this.proxyUri)) { handler.Proxy = new System.Net.WebProxy(this.proxyUri); handler.UseProxy = true; } if (request.Cookies != null && request.Cookies.HasKeys()) { handler.CookieContainer = new CookieContainer(); handler.CookieContainer.Add(baseUri, CreateCookieCollection(request.Cookies)); handler.UseCookies = true; } var httpClient = new HttpClient(handler); if (request.Headers != null && request.Headers.HasKeys()) { foreach (string key in request.Headers) { httpClient.DefaultRequestHeaders.Add(key, request.Headers[key]); } } if (!string.IsNullOrWhiteSpace(request.UserAgent)) { httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(this.userAgentMapping[request.UserAgent]); } if (hook != null) { hook(httpClient); } var message = isAsync ? await RequestAsync(httpClient, request.Method, requestUri, request.Content) : RequestAsync(httpClient, request.Method, requestUri, request.Content).Result; var httpResponse = new HttpResponse { Headers = CreateNameValueCollection(message.Headers), Cookies = CreateNameValueCollection(handler.CookieContainer.GetCookies(requestUri)), StatusCode = message.StatusCode, ContentType = message.Content.Headers.ContentType.ToString() }; var lastModified = message.Content.Headers.LastModified; if (lastModified != null) { httpResponse.LastModified = lastModified.Value.UtcDateTime; } httpResponse.Uri = message.RequestMessage.RequestUri; httpResponse.Contents = isAsync ? await message.Content.ReadAsStringAsync() : message.Content.ReadAsStringAsync().Result; return httpResponse; }
/// <summary> /// Gets response /// </summary> /// <param name="request">request information</param> /// <returns>response information</returns> public async Task <HttpResponse> GetResponseAsync(HttpRequest request, bool isAsync) { var baseUri = new Uri(this.baseUriMapping[request.BaseUri]); var requestUri = CreateUri(baseUri, request.PathInfos, request.QueryStrings, request.Fragment); request.Uri = requestUri.ToString(); var handler = new HttpClientHandler(); if (!string.IsNullOrWhiteSpace(this.proxyUri)) { handler.Proxy = new System.Net.WebProxy(this.proxyUri); handler.UseProxy = true; } if (request.Cookies != null && request.Cookies.HasKeys()) { handler.CookieContainer = new CookieContainer(); handler.CookieContainer.Add(baseUri, CreateCookieCollection(request.Cookies)); handler.UseCookies = true; } var httpClient = new HttpClient(handler); if (request.Headers != null && request.Headers.HasKeys()) { foreach (string key in request.Headers) { httpClient.DefaultRequestHeaders.Add(key, request.Headers[key]); } } if (!string.IsNullOrWhiteSpace(request.UserAgent)) { httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(this.userAgentMapping[request.UserAgent]); } var message = isAsync ? await RequestAsync(httpClient, request.Method, requestUri, request.Content) : RequestAsync(httpClient, request.Method, requestUri, request.Content).Result; var httpResponse = new HttpResponse { Headers = CreateNameValueCollection(message.Headers), Cookies = CreateNameValueCollection(handler.CookieContainer.GetCookies(requestUri)), StatusCode = message.StatusCode, ContentType = message.Content.Headers.ContentType.ToString() }; var lastModified = message.Content.Headers.LastModified; if (lastModified != null) { httpResponse.LastModified = lastModified.Value.UtcDateTime; } httpResponse.Uri = message.RequestMessage.RequestUri; httpResponse.Contents = isAsync ? await message.Content.ReadAsStringAsync() : message.Content.ReadAsStringAsync().Result; return(httpResponse); }