예제 #1
0
        private HttpRequestMessage CreateHttpRequestMessage(RestClientContext context, RestRequest restRequest)
        {
            var httpRequest = new HttpRequestMessage
            {
                RequestUri = restRequest.Uri,
                Method     = restRequest.Method,
                Content    = this.CreateHttpContent(restRequest),
            };

            httpRequest.Headers.Host = httpRequest.RequestUri.Host;
            httpRequest.Headers.UserAgent.Add(new ProductInfoHeaderValue("GoLava", "1.0"));
            httpRequest.Headers.Add("Connection", "keep-alive");

            var cookies = context.CookieJar.GetRequestHeaderValue(httpRequest.RequestUri);

            if (!string.IsNullOrEmpty(cookies))
            {
                httpRequest.Headers.Add("Cookie", cookies);
            }

            if (restRequest.Headers != null)
            {
                foreach (var header in restRequest.Headers)
                {
                    httpRequest.Headers.TryAddWithoutValidation(header.Key, header.Value);
                }
            }

            return(httpRequest);
        }
예제 #2
0
        public async Task <RestResponse> SendAsync(RestClientContext context, RestRequest request)
        {
            await Configure.AwaitFalse();

            var httpRequest  = this.CreateHttpRequestMessage(context, request);
            var httpResponse = await _httpClient.SendAsync(httpRequest);

            var restResponse = await this.CreateRestResponseAsync <RestResponse>(context, httpRequest, httpResponse);

            return(restResponse);
        }
예제 #3
0
        private async Task <RestResponse <TContent> > CreateRestContentResponseAsync <TContent>(
            RestClientContext context, HttpRequestMessage httpRequest, HttpResponseMessage httpResponse)
        {
            await Configure.AwaitFalse();

            var restResponse = await this.CreateRestResponseAsync <RestResponse <TContent> >(context, httpRequest, httpResponse);

            if (httpResponse.IsSuccessStatusCode)
            {
                if (restResponse.ContentType == RestContentType.Json)
                {
                    var content = this.Serializer.Deserialize <TContent>(restResponse.RawContent.ToString());
                    restResponse.Content = content;
                }
            }
            return(restResponse);
        }
예제 #4
0
        private async Task <TRestResponse> CreateRestResponseAsync <TRestResponse>(
            RestClientContext context, HttpRequestMessage httpRequest, HttpResponseMessage httpResponse)
            where TRestResponse : RestResponse, new()
        {
            await Configure.AwaitFalse();

            var restResponse = new TRestResponse
            {
                StatusCode = httpResponse.StatusCode,
                Headers    = new RestHeaders(httpResponse.Headers)
            };

            var httpContent = httpResponse.Content;

            if (httpContent != null)
            {
                restResponse.ContentType = ConvertContentType(httpContent.Headers);
                if (restResponse.ContentType == RestContentType.Binary)
                {
                    var rawContent = await httpContent.ReadAsByteArrayAsync();

                    restResponse.RawContent = new RawBinaryContent(rawContent);
                }
                else
                {
                    var rawContent = await httpContent.ReadAsStringAsync();

                    restResponse.RawContent = new RawStringContent(rawContent);
                }
            }
            if (httpResponse.IsSuccessStatusCode)
            {
                if (httpResponse.Headers.TryGetValues("set-cookie", out IEnumerable <string> cookieHeaders))
                {
                    context.CookieJar.Add(httpRequest.RequestUri, cookieHeaders);
                }
            }
            return(restResponse);
        }