Exemplo n.º 1
0
        public async Task <IConnectionResponse> SendAsync(Stream body = null)
        {
            handlerHook(handler, settings);

            if (body != null)
            {
                messageHook = (messageRequest, s) =>
                {
                    messageRequest.Content = new StreamContent(body);
                    messageRequest.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
                } +messageHook;
            }

            HttpRequestMessage msg = new HttpRequestMessage();

            messageHook(msg, settings);


            clientHook(httpClient, settings);
            var httpResponse = await httpClient.SendAsync(msg);

            responseHook(httpResponse, settings);
            var response = new PrivateResponse(httpResponse);

            if ((successCondition != null && successCondition((int)httpResponse.StatusCode)) || (successCondition == null && httpResponse.IsSuccessStatusCode))
            {
                return(response);
            }
            else
            {
                throw new ConnectionResponseException(response, "Success conditions not met");
            }
        }
Exemplo n.º 2
0
        public async Task <IConnectionResponse> SendAsync(Stream body = null)
        {
            logger.LogTrace("Sending HTTP Request");
            handlerHook(handler, settings);

            if (body != null)
            {
                messageHook = (messageRequest, s) =>
                {
                    messageRequest.Content = new StreamContent(body);
                    messageRequest.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType)
                    {
                        CharSet = "utf-8"
                    };
                } +messageHook;
            }

            HttpRequestMessage msg = new HttpRequestMessage();

            messageHook(msg, settings);

            clientHook(httpClient, settings);

            logger.LogDebug("Uri: {0} {1}", msg.RequestUri, msg.Method);
            logger.LogTrace("Request Cookies State: {0}", handler.CookieContainer.GetCookies(settings.ServerUrl).AllToString());

            var httpResponse = await httpClient.SendAsync(msg);

            logger.LogDebug("Response Cookies State: {0}", handler.CookieContainer.GetCookies(settings.ServerUrl).AllToString());
            responseHook(httpResponse, settings);
            var response = new PrivateResponse(httpResponse, logger);

            if ((successCondition != null && successCondition((int)httpResponse.StatusCode)) || (successCondition == null && httpResponse.IsSuccessStatusCode))
            {
                // log location header
                if (httpResponse.Headers.Location != null)
                {
                    logger.LogDebug("Status code: {0}, Location: {1}", httpResponse.StatusCode, httpResponse.Headers.Location);
                }
                return(response);
            }
            else
            {
                logger.LogInformation($"Condition not met, failing. HTTP Response code: {response.Code}");
                throw new ConnectionResponseException(response, "Success conditions not met");
            }
        }