示例#1
0
        /// <summary>
        /// Fires RequestSending event and output standard log to console
        /// </summary>
        protected virtual void OnRequestSending(RequestSendingEventArgs e)
        {
            if (RequestSending != null)
            {
                RequestSending(this, e);
            }

            this._log.WriteLine("-------------------------------- [REQUEST] ------------------------------");

            if (e.Request != null)
            {
                this._log.WriteLine("[URL]: " + e.Request.RequestUri.AbsoluteUri);
                this._log.WriteLine("[Method]: " + e.Request.Method);

                this._log.WriteLine("[Headers]: ");
                if (e.Request.Headers != null)
                {
                    foreach (string s in e.Request.Headers.AllKeys)
                    {
                        this._log.WriteLine(s + ": " + e.Request.Headers[s]);
                    }
                }

                if (e.Request.Method.Equals("post", StringComparison.OrdinalIgnoreCase) ||
                    e.Request.Method.Equals("put", StringComparison.OrdinalIgnoreCase))
                {
                    this._log.WriteLine("[Request Data]:");
                    this._log.WriteLine((e.RequestData.Length > 1000) ? e.RequestData.Substring(0, 1000) + " \n [Too long request content....]"
                                                                      : e.RequestData);
                }
            }
        }
示例#2
0
        /// <summary>
        /// Initializes and executes request and handles response.
        /// </summary>
        internal BrowserInfo ExecuteRequest(string url, string method, string contentType, PostDataCollection postData)
        {
            BrowserInfo browserInfo = new BrowserInfo();

            HttpWebRequest request = this._webRequestor.CreateRequest(url);

            request.Method = method.ToUpperInvariant();

            // Note: cookies should be set before ContentType (request reorganize headers and cookies after that)
            request.AddCookies(this._currentCookies);

            // Note: content type (and length) should be set before data sent to the request stream
            if (!string.IsNullOrEmpty(contentType))
            {
                request.ContentType = contentType;
            }

            string requestData = "";

            if ((request.Method.Equals("post", StringComparison.OrdinalIgnoreCase) ||
                 request.Method.Equals("put", StringComparison.OrdinalIgnoreCase)) &&
                postData != null)
            {
                if (postData.HasFilesToUpload)
                {
                    //request.KeepAlive = true;
                    // Note: AddMultipartRequestData sets corresponding ContentType
                    requestData = request.AddMultipartRequestData(postData, this._fileSystem);
                }
                else
                {
                    requestData = request.AddStandardRequestData(postData.GetPostDataString());
                }
            }

            // fire event before request has been sent
            RequestSendingEventArgs args = new RequestSendingEventArgs(request, requestData);

            OnRequestSending(args);
            request = args.Request;

            HttpWebResponse response = null;

            try
            {
                // execute request
                response = this._webRequestor.ExecuteRequest(request);
            }
            catch (WebException e)
            {
                // We catch only WebException here. In this case there should be valid error page,
                // which we want to obtain from the Response. If there was different type of exception,
                // or we have another exception while reading response, then we let it go through.
                if (e.Response != null)
                {
                    response = (HttpWebResponse)e.Response;
                }
                else
                {
                    throw;
                }
            }

            // get raw body
            string body = GetStringFromStream(response.GetResponseStream(), Encoding.UTF8);

            // fire event after responce has been received
            OnResponseReceived(new ResponseReceivedEventArgs(response, body));

            if (!string.IsNullOrEmpty(body))
            {
                // store response body
                browserInfo.Data = "<BrowserEmulatorRoot>" + body + "</BrowserEmulatorRoot>";

                // remember current URL
                this._currentUri = response.ResponseUri;

                return(browserInfo);
            }

            throw new InvalidOperationException("Response did not contain html markup.");
        }