示例#1
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.");
        }