Example #1
0
        /// <summary>
        /// Raises the <see cref="SendingRemoteHttpRequest"/> event.
        /// </summary>
        /// <param name="eventArgs">A <see cref="SendingRemoteHttpRequestEventArgs"/> that contains the event data.</param>
        protected virtual void OnSendingRemoteHttpRequest(SendingRemoteHttpRequestEventArgs eventArgs)
        {
            if (eventArgs == null)
            {
                throw new ArgumentNullException("eventArgs", "eventArgs must not be null");
            }

            if (this.SendingRemoteHttpRequest != null)
            {
                this.SendingRemoteHttpRequest(this, eventArgs);
            }
        }
Example #2
0
        private async Task <HttpResponseInfo> MakeHttpRequest(HttpRequestInfo requestInfo)
        {
            SendingRemoteHttpRequestEventArgs eventArgs = new SendingRemoteHttpRequestEventArgs(requestInfo.HttpMethod, requestInfo.FullUri.ToString(), requestInfo.RequestBody);

            this.OnSendingRemoteHttpRequest(eventArgs);

            HttpMethod method = new HttpMethod(requestInfo.HttpMethod);

            using (HttpRequestMessage requestMessage = new HttpRequestMessage(method, requestInfo.FullUri))
            {
                foreach (KeyValuePair <string, string> header in eventArgs.Headers)
                {
                    requestMessage.Headers.Add(header.Key, header.Value);
                }

                if (requestInfo.HttpMethod == HttpCommandInfo.GetCommand)
                {
                    CacheControlHeaderValue cacheControlHeader = new CacheControlHeaderValue();
                    cacheControlHeader.NoCache          = true;
                    requestMessage.Headers.CacheControl = cacheControlHeader;
                }

                if (requestInfo.HttpMethod == HttpCommandInfo.PostCommand)
                {
                    MediaTypeWithQualityHeaderValue acceptHeader = new MediaTypeWithQualityHeaderValue(JsonMimeType);
                    acceptHeader.CharSet = Utf8CharsetType;
                    requestMessage.Headers.Accept.Add(acceptHeader);

                    byte[] bytes = Encoding.UTF8.GetBytes(eventArgs.RequestBody);
                    requestMessage.Content = new ByteArrayContent(bytes, 0, bytes.Length);

                    MediaTypeHeaderValue contentTypeHeader = new MediaTypeHeaderValue(JsonMimeType);
                    contentTypeHeader.CharSet = Utf8CharsetType;
                    requestMessage.Content.Headers.ContentType = contentTypeHeader;
                }

                using (HttpResponseMessage responseMessage = await this.client.SendAsync(requestMessage))
                {
                    HttpResponseInfo httpResponseInfo = new HttpResponseInfo();
                    httpResponseInfo.Body = await responseMessage.Content.ReadAsStringAsync();

                    httpResponseInfo.ContentType = responseMessage.Content.Headers.ContentType.ToString();
                    httpResponseInfo.StatusCode  = responseMessage.StatusCode;

                    return(httpResponseInfo);
                }
            }
        }
Example #3
0
        private HttpResponseInfo MakeHttpRequest(HttpRequestInfo requestInfo)
        {
            HttpWebRequest request = HttpWebRequest.Create(requestInfo.FullUri) as HttpWebRequest;

            if (!string.IsNullOrEmpty(requestInfo.FullUri.UserInfo) && requestInfo.FullUri.UserInfo.Contains(":"))
            {
                string[] userInfo = this.remoteServerUri.UserInfo.Split(new char[] { ':' }, 2);
                request.Credentials     = new NetworkCredential(userInfo[0], userInfo[1]);
                request.PreAuthenticate = true;
            }

            string userAgentString = string.Format(CultureInfo.InvariantCulture, UserAgentHeaderTemplate, ResourceUtilities.AssemblyVersion, ResourceUtilities.PlatformFamily);

            request.UserAgent = userAgentString;
            request.Method    = requestInfo.HttpMethod;
            request.Timeout   = (int)this.serverResponseTimeout.TotalMilliseconds;
            request.Accept    = RequestAcceptHeader;
            request.KeepAlive = this.enableKeepAlive;
            request.Proxy     = this.proxy;
            request.ServicePoint.ConnectionLimit = 2000;
            if (request.Method == CommandInfo.GetCommand)
            {
                request.Headers.Add("Cache-Control", "no-cache");
            }

            SendingRemoteHttpRequestEventArgs eventArgs = new SendingRemoteHttpRequestEventArgs(request, requestInfo.RequestBody);

            this.OnSendingRemoteHttpRequest(eventArgs);

            if (request.Method == CommandInfo.PostCommand)
            {
                string payload = eventArgs.RequestBody;
                byte[] data    = Encoding.UTF8.GetBytes(payload);
                request.ContentType = ContentTypeHeader;
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
            }

            HttpResponseInfo responseInfo = new HttpResponseInfo();
            HttpWebResponse  webResponse  = null;

            try
            {
                webResponse = request.GetResponse() as HttpWebResponse;
            }
            catch (WebException ex)
            {
                webResponse = ex.Response as HttpWebResponse;
                if (ex.Status == WebExceptionStatus.Timeout)
                {
                    string timeoutMessage = "The HTTP request to the remote WebDriver server for URL {0} timed out after {1} seconds.";
                    throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, timeoutMessage, request.RequestUri.AbsoluteUri, this.serverResponseTimeout.TotalSeconds), ex);
                }
                else if (ex.Response == null)
                {
                    string nullResponseMessage = "A exception with a null response was thrown sending an HTTP request to the remote WebDriver server for URL {0}. The status of the exception was {1}, and the message was: {2}";
                    throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, nullResponseMessage, request.RequestUri.AbsoluteUri, ex.Status, ex.Message), ex);
                }
            }

            if (webResponse == null)
            {
                throw new WebDriverException("No response from server for url " + request.RequestUri.AbsoluteUri);
            }
            else
            {
                responseInfo.Body        = GetTextOfWebResponse(webResponse);
                responseInfo.ContentType = webResponse.ContentType;
                responseInfo.StatusCode  = webResponse.StatusCode;
            }

            return(responseInfo);
        }