示例#1
0
        public static HttpStatusCode?GetStatusCode(IHttpWebRequest webRequest)
        {
            if (webRequest is null)
            {
                throw new ArgumentNullException(nameof(webRequest));
            }

            HttpStatusCode?statusCode = null;

            // Attempt to make a HEAD request first. If it fails, we'll fall back to making a GET request.

            webRequest.AllowAutoRedirect = false;

            foreach (string method in new[] { "HEAD", "GET" })
            {
                webRequest.Method = method;

                try {
                    using (WebResponse webResponse = webRequest.GetResponse())
                        statusCode = (webResponse as IHttpWebResponse)?.StatusCode;
                }
                catch (WebException ex) {
                    using (WebResponse webResponse = ex.Response)
                        statusCode = (webResponse as IHttpWebResponse)?.StatusCode;
                }

                if (statusCode.HasValue && statusCode != HttpStatusCode.MethodNotAllowed)
                {
                    break;
                }
            }

            return(statusCode);
        }
示例#2
0
        /// <summary>
        /// Execute request and returns entityResponse.
        /// </summary>
        /// <param name="httpWebRequest">Http web request.</param>
        /// <returns></returns>
        protected IHttpWebResponse ExecuteRequest(IHttpWebRequest httpWebRequest)
        {
            this.PreExecuteRequest(httpWebRequest);
            IHttpWebResponse httpWebResponse = httpWebRequest.GetResponse();

            this.ProcessHttpWebResponseInternal(httpWebResponse);
            return(httpWebResponse);
        }
示例#3
0
        public static long GetRemoteFileSize(IHttpWebRequest httpWebRequest)
        {
            if (httpWebRequest is null)
            {
                throw new ArgumentNullException(nameof(httpWebRequest));
            }

            long?        fileSize      = null;
            WebException lastException = null;

            string originalMethod = httpWebRequest.Method;

            // Attempt to make a HEAD request first. If it fails, we'll fall back to making a GET request.

            foreach (string method in new[] { "HEAD", "GET" })
            {
                httpWebRequest.Method = method;

                try {
                    using (WebResponse webResponse = httpWebRequest.GetResponse()) {
                        if (webResponse.Headers.TryGetHeader(HttpResponseHeader.ContentLength, out string contentLength) &&
                            long.TryParse(contentLength, NumberStyles.Integer, CultureInfo.InvariantCulture, out long parsedContentLength))
                        {
                            fileSize = parsedContentLength;
                        }

                        // We'll exit the loop regardless of whether or not we got a content-length header, because trying again with a GET request is unlikely to produce one.

                        break;
                    }
                }
                catch (WebException ex) {
                    lastException = ex;

                    // 405 error just indicates that HEAD requests aren't supported by the server, so we shouldn't throw yet (this is a common case).
                    // We'll follow up by attempting a GET request instead.

                    using (WebResponse webResponse = ex.Response)
                        if ((webResponse as IHttpWebResponse)?.StatusCode != HttpStatusCode.MethodNotAllowed)
                        {
                            throw ex;
                        }
                }
            }

            // Restore original configuration.

            httpWebRequest.Method = originalMethod;

            if (fileSize.HasValue)
            {
                return(fileSize.Value);
            }
            else
            {
                throw lastException ?? new Exception($"Unable to obtain file size from {httpWebRequest.RequestUri}.");
            }
        }
示例#4
0
        // Public members

        public static HttpStatusCode GetStatusCode(IHttpWebRequest httpWebRequest)
        {
            if (httpWebRequest is null)
            {
                throw new ArgumentNullException(nameof(httpWebRequest));
            }

            HttpStatusCode?statusCode    = null;
            WebException   lastException = null;

            bool   originalAllowAutoRedirect = httpWebRequest.AllowAutoRedirect;
            string originalMethod            = httpWebRequest.Method;

            httpWebRequest.AllowAutoRedirect = false;

            // Attempt to make a HEAD request first. If it fails, we'll fall back to making a GET request.

            foreach (string method in new[] { "HEAD", "GET" })
            {
                httpWebRequest.Method = method;

                try {
                    using (WebResponse webResponse = httpWebRequest.GetResponse())
                        statusCode = (webResponse as IHttpWebResponse)?.StatusCode;
                }
                catch (WebException ex) {
                    // ex.Response will be a regular HttpWebResponse instead of an IHttpWebResponse when the request fails.

                    using (WebResponse webResponse = ex.Response)
                        statusCode = (webResponse as HttpWebResponse)?.StatusCode;

                    lastException = ex;
                }

                if (statusCode.HasValue && statusCode != HttpStatusCode.MethodNotAllowed)
                {
                    break;
                }
            }

            // Restore original configuration.

            httpWebRequest.AllowAutoRedirect = originalAllowAutoRedirect;
            httpWebRequest.Method            = originalMethod;

            if (statusCode.HasValue)
            {
                return(statusCode.Value);
            }
            else
            {
                throw lastException ?? new Exception($"Unable to obtain HTTP status code from {httpWebRequest.RequestUri}.");
            }
        }
示例#5
0
 /// <summary>
 /// Gets http response, returns (etag, data)
 /// </summary>
 /// <param name="request"></param>
 /// <returns></returns>
 private SolrResponse GetResponse(IHttpWebRequest request)
 {
     using (var response = request.GetResponse()) {
         var etag         = response.Headers[HttpResponseHeader.ETag];
         var cacheControl = response.Headers[HttpResponseHeader.CacheControl];
         if (cacheControl != null && cacheControl.Contains("no-cache"))
         {
             etag = null; // avoid caching things marked as no-cache
         }
         return(new SolrResponse(etag, ReadResponseToString(response)));
     }
 }
示例#6
0
        public virtual void GetResponse(IHttpWebRequest request, string filename, bool streamResponse)
        {
            try
            {
                _response = request.GetResponse();
            }
            catch (WebException webException)
            {
                if (webException.Response == null)
                {
                    throw;
                }
                _response = new HttpWebResponseWrapper((HttpWebResponse)webException.Response);
            }

            GetHeaders();

            if (streamResponse)
            {
                return;
            }

            using (var stream = _response.GetResponseStream())
            {
                if (stream == null)
                {
                    return;
                }

                if (!string.IsNullOrEmpty(filename))
                {
                    using (var filestream = new FileStream(filename, FileMode.CreateNew))
                    {
                        int count;
                        var buffer = new byte[8192];

                        while ((count = stream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            filestream.Write(buffer, 0, count);
                        }
                    }
                }
                else
                {
                    var encoding = string.IsNullOrEmpty(CharacterSet) ? Encoding.UTF8 : Encoding.GetEncoding(CharacterSet);
                    using (var reader = new StreamReader(stream, encoding))
                    {
                        RawText = reader.ReadToEnd();
                    }
                }
            }
        }
示例#7
0
        public static DateTimeOffset GetRemoteDateTime(IHttpWebRequest httpWebRequest)
        {
            string originalMethod = httpWebRequest.Method;

            try {
                using (WebResponse webResponse = httpWebRequest.GetResponse()) {
                    if (webResponse.Headers.TryGetHeader(HttpResponseHeader.Date, out string date) && DateUtilities.TryParseHttpHeader(date, out DateTimeOffset parsedDate))
                    {
                        return(parsedDate);
                    }
                }
            }
            finally {
                httpWebRequest.Method = originalMethod;
            }

            throw new Exception($"Unable to obtain date from {httpWebRequest.RequestUri}.");
        }
示例#8
0
        /// <summary>
        /// Gets response stream from a webrequest using the correct encoding. If the encoding is not
        /// specified, then the encoding will be detected from the BOM.
        /// </summary>
        ///
        /// <param name="request">
        /// The request.
        /// </param>
        ///
        /// <returns>
        /// The response stream.
        /// </returns>

        protected StreamReader GetResponseStreamReader(IHttpWebRequest request)
        {
            var response = request.GetResponse();

            StreamReader reader;
            var          encoding = GetEncoding(response);

            if (encoding != null)
            {
                reader = new StreamReader(response.GetResponseStream(), encoding);
            }
            else
            {
                // when no encoding was specified on the HTTP response, just use BOM, and fall back on UTF8.

                reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8, true);
            }
            return(reader);
        }
示例#9
0
        string createResponse(IHttpWebRequest request)
        {
            var responseValue         = string.Empty;
            IHttpWebResponse response = null;

            try {
                response = request.GetResponse();
                if (response == null)
                {
                    throw new Error.ApiConnectionError(Constants.Messages.ApiConnectionError, null, null);
                }
                responseValue = ParseResponse(response);
            } catch (WebException e) {
                response      = request.SetErrorResponse(e.Response);
                responseValue = ParseResponse(response);
                HandleErrors(response.StatusCode, responseValue);
            }

            return(responseValue);
        }
示例#10
0
        private HttpWebHelperResult GetResponse(IHttpWebRequest httpWebRequest, Stream requestBody)
        {
            IHttpWebResponse    httpWebResponse          = null;
            Exception           exception                = null;
            HttpWebHelperResult httpWebHelperAsyncResult = null;

            try
            {
                httpWebResponse = httpWebRequest.GetResponse();
            }
            catch (WebException ex)
            {
                var webException = new WebExceptionWrapper(ex);
                httpWebResponse = webException.GetResponse();
                exception       = webException;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            finally
            {
                if (httpWebResponse != null)
                {
                    var args = new ResponseReceivedEventArgs(httpWebResponse, exception, null);
                    OnResponseReceived(args);
                    httpWebHelperAsyncResult = ReadResponseStream(httpWebRequest, httpWebResponse, exception, args.ResponseSaveStream);
                }
                else
                {
                    httpWebHelperAsyncResult = new HttpWebHelperResult(httpWebRequest, httpWebResponse, exception, null, false, true, null, null);
                }
            }

            return(httpWebHelperAsyncResult);
        }
示例#11
0
        /// <summary>
        /// Gets http response, returns (etag, data)
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private SolrResponse GetResponse(IHttpWebRequest request) {
            using (var response = request.GetResponse()) {
                var etag = response.Headers[HttpResponseHeader.ETag];
                var cacheControl = response.Headers[HttpResponseHeader.CacheControl];
                if (cacheControl != null && cacheControl.Contains("no-cache"))
                    etag = null; // avoid caching things marked as no-cache

                return new SolrResponse(etag, ReadResponseToString(response));
            }
        }
示例#12
0
        /// <summary>
        /// Gets response stream from a webrequest using the correct encoding.
        /// </summary>
        ///
        /// <param name="request">
        /// The request.
        /// </param>
        ///
        /// <returns>
        /// The response stream.
        /// </returns>

        protected StreamReader GetResponseStreamReader(IHttpWebRequest request) {
            var response = request.GetResponse();
            
            //var response = request.GetResponse();
            //var encoding = response.Headers["
        
            StreamReader reader;
            var encoding = String.IsNullOrEmpty(response.CharacterSet) ?
                Encoding.UTF8 :
                Encoding.GetEncoding(response.CharacterSet);

            reader = new StreamReader(response.GetResponseStream(), encoding);
            return reader;
        }
示例#13
0
        internal bool DoRequest(Uri uri, string method, NameValueCollection userVariables, string postData, string contentType, string encodingType, int timeoutMilliseconds)
        {
            string html;
            string referer = null;

            if (uri.IsFile)
            {
                StreamReader reader = new StreamReader(uri.AbsolutePath);
                html = reader.ReadToEnd();
                reader.Close();
            }
            else
            {
                bool   handle3xxRedirect = false;
                int    maxRedirects      = 5;         // Per RFC2068, Section 10.3
                string postBody          = string.Empty;
                do
                {
                    Debug.WriteLine(uri.ToString());
                    if (maxRedirects-- == 0)
                    {
                        Log("Too many 3xx redirects", LogMessageType.Error);
                        return(false);
                    }

                    handle3xxRedirect = false;
                    IHttpWebRequest req = null;

                    try
                    {
                        req = PrepareRequestObject(uri, method, contentType, timeoutMilliseconds);
                    }
                    catch (NotSupportedException)
                    {
                        // Happens when the URL cannot be parsed (example: 'javascript:')
                        return(false);
                    }

                    foreach (var header in _extraHeaders)
                    {
                        if (header.StartsWith("host:", StringComparison.OrdinalIgnoreCase))
                        {
                            req.Host = header.Split(':')[1];
                        }
                        else
                        {
                            req.Headers.Add(header);
                        }
                    }

                    if (!string.IsNullOrEmpty(encodingType))
                    {
                        req.Headers.Add(HttpRequestHeader.ContentEncoding, encodingType);
                    }

                    // Remove all expired basic authentication tokens
                    List <BasicAuthenticationToken> expired =
                        _basicAuthenticationTokens.Values.Where(t => DateTime.Now > t.Expiration).ToList();
                    foreach (var expiredToken in expired)
                    {
                        _basicAuthenticationTokens.Remove(expiredToken.Domain);
                    }

                    // If an authentication token exists for the domain, add the authorization header.
                    foreach (var token in _basicAuthenticationTokens.Values)
                    {
                        if (req.Host.Contains(token.Domain))
                        {
                            // Extend the expiration.
                            token.UpdateExpiration();

                            // Add the authentication header.
                            req.Headers.Add(string.Format(
                                                "Authorization: Basic {0}",
                                                token.Token));
                        }
                    }

                    if (_includeFormValues != null)
                    {
                        if (userVariables == null)
                        {
                            userVariables = _includeFormValues;
                        }
                        else
                        {
                            userVariables.Add(_includeFormValues);
                        }
                    }

                    if (userVariables != null)
                    {
                        if (method == "POST")
                        {
                            postBody = StringUtil.MakeQueryString(userVariables);
                            byte[] data = Encoding.GetEncoding(28591).GetBytes(postBody);
                            req.ContentLength = data.Length;
                            using (Stream stream = req.GetRequestStream())
                            {
                                stream.Write(data, 0, data.Length);
                            }
                        }
                        else
                        {
                            uri = new Uri(
                                uri.Scheme + "://" + uri.Host + ":" + uri.Port + uri.AbsolutePath
                                + ((userVariables.Count > 0) ? "?" + StringUtil.MakeQueryString(userVariables) : "")
                                );
                            req = PrepareRequestObject(uri, method, contentType, timeoutMilliseconds);
                        }
                    }
                    else if (postData != null)
                    {
                        if (method == "GET")
                        {
                            throw new InvalidOperationException("Cannot call DoRequest with method GET and non-null postData");
                        }
                        postBody = postData;
                        byte[] data = Encoding.GetEncoding(28591).GetBytes(postData);
                        req.ContentLength = data.Length;
                        using (Stream stream = req.GetRequestStream())
                        {
                            stream.Write(data, 0, data.Length);
                        }
                    }

                    referer = req.Referer;

                    if (contentType != null)
                    {
                        req.ContentType = contentType;
                    }

                    _lastRequestLog = new HttpRequestLog
                    {
                        Method         = method,
                        PostData       = userVariables,
                        PostBody       = postBody,
                        RequestHeaders = req.Headers,
                        Url            = uri
                    };
                    try
                    {
                        using (IHttpWebResponse response = req.GetResponse())
                        {
                            Encoding responseEncoding = Encoding.UTF8;                             //default
                            string   charSet          = response.CharacterSet;
                            if (!String.IsNullOrEmpty(charSet))
                            {
                                try
                                {
                                    responseEncoding = Encoding.GetEncoding(charSet);
                                }
                                catch (ArgumentException)
                                {
                                    responseEncoding = Encoding.UTF8;                                     // try using utf8
                                }
                            }

                            //ensure the stream is disposed
                            using (Stream rs = response.GetResponseStream())
                            {
                                using (StreamReader reader = new StreamReader(rs, responseEncoding))
                                {
                                    html = reader.ReadToEnd();
                                }
                            }

                            _doc = null;
                            _includeFormValues = null;

                            _lastRequestLog.Text            = html;
                            _lastRequestLog.ResponseHeaders = response.Headers;
                            _lastRequestLog.ResponseCode    = (int)response.StatusCode;

                            if (method == "GET" && uri.Query.Length > 0 && uri.Query != "?")
                            {
                                _lastRequestLog.QueryStringData = HttpUtility.ParseQueryString(uri.Query);
                            }

                            if (AutoRedirect == true &&
                                (((int)response.StatusCode == 300 ||                                 // Not entirely supported. If provided, the server's preference from the Location header is honored.
                                  (int)response.StatusCode == 301 ||
                                  (int)response.StatusCode == 302 ||
                                  (int)response.StatusCode == 303 ||
                                  // 304 - Unsupported, conditional Get requests are not supported (mostly because SimpleBrowser does not cache content)
                                  // 305 - Unsupported, possible security threat
                                  // 306 - No longer used, per RFC2616, Section 10.3.7
                                  (int)response.StatusCode == 307 ||
                                  (int)response.StatusCode == 308) &&
                                 response.Headers.AllKeys.Contains("Location")))
                            {
                                uri = new Uri(uri, response.Headers["Location"]);
                                handle3xxRedirect = true;
                                Debug.WriteLine("Redirecting to: " + uri);
                                method        = "GET";
                                postData      = null;
                                userVariables = null;
                            }

                            if (response.Headers.AllKeys.Contains("Set-Cookie"))
                            {
                                var cookies = SetCookieHeaderParser.GetAllCookiesFromHeader(uri.Host, response.Headers["Set-Cookie"]);
                                Cookies.Add(cookies);
                            }
                        }
                    }
                    catch (WebException ex)
                    {
                        if (ex.Response != null)
                        {
                            _lastRequestLog.ResponseHeaders = ex.Response.Headers;
                            //ensure the stream is disposed
                            using (Stream rs = ex.Response.GetResponseStream())
                            {
                                using (StreamReader reader = new StreamReader(rs))
                                {
                                    html = reader.ReadToEnd();
                                }
                            }
                            _lastRequestLog.Text = html;
                        }

                        LastWebException = ex;

                        switch (ex.Status)
                        {
                        case WebExceptionStatus.Timeout:
                            Log("A timeout occurred while trying to load the web page", LogMessageType.Error);
                            break;

                        case WebExceptionStatus.ReceiveFailure:
                            Log("The response was cut short prematurely", LogMessageType.Error);
                            break;

                        default:
                            Log("An exception was thrown while trying to load the page: " + ex.Message, LogMessageType.Error);
                            break;
                        }
                        return(false);
                    }
                    finally
                    {
                        LogRequestData();
                    }
                }while (handle3xxRedirect);
            }

            this._navigationAttributes = null;
            this.RemoveChildBrowsers();             //Any frames contained in the previous state should be removed. They will be recreated if we ever navigate back
            this.AddNavigationState(
                new NavigationState()
            {
                Html        = html,
                Url         = uri,
                ContentType = contentType,
                Referer     = string.IsNullOrEmpty(referer) ? null : new Uri(Uri.UnescapeDataString(referer))
            });

            return(true);
        }
示例#14
0
        /// <summary>
        /// Gets response stream from a webrequest using the correct encoding. If the encoding is not
        /// specified, then the encoding will be detected from the BOM.
        /// </summary>
        ///
        /// <param name="request">
        /// The request.
        /// </param>
        ///
        /// <returns>
        /// The response stream.
        /// </returns>

        protected StreamReader GetResponseStreamReader(IHttpWebRequest request)
        {
            var response = request.GetResponse();

            StreamReader reader;
            var encoding = GetEncoding(response);
            if (encoding != null)
            {
                reader = new StreamReader(response.GetResponseStream(), encoding);
            }
            else
            {
                // when no encoding was specified on the HTTP response, just use BOM, and fall back on UTF8.
                
                reader = new StreamReader(response.GetResponseStream(),Encoding.UTF8, true);
            }
            return reader;
        }
示例#15
0
        internal bool DoRequest(Uri uri, string method, NameValueCollection userVariables, string postData, string contentType, string encodingType, int timeoutMilliseconds)
        {
            /* IMPORTANT INFORMATION:
             * HttpWebRequest has a bug where if a 302 redirect is encountered (such as from a Response.Redirect), any cookies
             * generated during the request are ignored and discarded during the internal redirect process. The headers are in
             * fact returned, but the normal process where the cookie headers are turned into Cookie objects in the cookie
             * container is skipped, thus breaking the login processes of half the sites on the internet.
             *
             * The workaround is as follows:
             * 1. Turn off AllowAutoRedirect so we can intercept the redirect and do things manually
             * 2. Read the Set-Cookie headers from the response and manually insert them into the cookie container
             * 3. Get the Location header and redirect to the location specified in the "Location" response header
             *
             * Worth noting that even if this bug has been solved in .Net 4 (I haven't checked) we should still use manual
             * redirection so that we can properly log responses.
             *
             * OBSOLETE ISSUE: (Bug has been resolved in the .Net 4 framework, which this library is now targeted at)
             * //CookieContainer also has a horrible bug relating to the specified cookie domain. Basically, if it contains
             * //a cookie where the "domain" token is specified as ".domain.xxx" and you attempt to request http://domain.ext,
             * //the cookies are not retrievable via that Uri, as you would expect them to be. CookieContainer is incorrectly
             * //assuming that the leading dot is a prerequisite specifying that a subdomain is required as opposed to the
             * //correct behaviour which would be to take it to mean that the domain and all subdomains are valid for the cookie.
             * //http://channel9.msdn.com/forums/TechOff/260235-Bug-in-CookieContainer-where-do-I-report/?CommentID=397315
             * //The workaround is as follows:
             * //When retrieving the response, iterate through the Set-Cookie header and any cookie that explicitly sets
             * //the domain token with a leading dot should also set the cookie without the leading dot.
             */

            bool   handle301Or302Redirect;
            int    maxRedirects = 10;
            string html;
            string responseContentType;
            string postBody = "";

            do
            {
                Debug.WriteLine(uri.ToString());
                if (maxRedirects-- == 0)
                {
                    Log("Too many 302 redirects", LogMessageType.Error);
                    return(false);
                }
                handle301Or302Redirect = false;
                IHttpWebRequest req = null;
                try
                {
                    req = PrepareRequestObject(uri, method, contentType, timeoutMilliseconds);
                }
                catch (NotSupportedException e)
                {
                    // Happens when the URL cannot be parsed (example: 'javascript:')
                    return(false);
                }
                foreach (var header in _extraHeaders)
                {
                    req.Headers.Add(header);
                }
                if (encodingType != null)
                {
                    req.Headers.Add(HttpRequestHeader.ContentEncoding, encodingType);
                }
                if (_includeFormValues != null)
                {
                    if (userVariables == null)
                    {
                        userVariables = _includeFormValues;
                    }
                    else
                    {
                        userVariables.Add(_includeFormValues);
                    }
                }

                if (userVariables != null)
                {
                    if (method == "POST")
                    {
                        postBody = StringUtil.MakeQueryString(userVariables);
                        byte[] data = Encoding.GetEncoding(28591).GetBytes(postBody);
                        req.ContentLength = data.Length;
                        Stream stream = req.GetRequestStream();
                        stream.Write(data, 0, data.Length);
                        stream.Close();
                    }
                    else
                    {
                        uri = new Uri(
                            uri.Scheme + "://" + uri.Host + ":" + uri.Port + uri.AbsolutePath
                            + ((userVariables.Count > 0) ? "?" + StringUtil.MakeQueryString(userVariables) : "")
                            );
                        req = PrepareRequestObject(uri, method, contentType, timeoutMilliseconds);
                    }
                }
                else if (postData != null)
                {
                    if (method == "GET")
                    {
                        throw new InvalidOperationException("Cannot call DoRequest with method GET and non-null postData");
                    }
                    postBody = postData;
                    byte[] data = Encoding.GetEncoding(28591).GetBytes(postData);
                    req.ContentLength = data.Length;
                    Stream stream = req.GetRequestStream();
                    stream.Write(data, 0, data.Length);
                    stream.Close();
                }

                if (contentType != null)
                {
                    req.ContentType = contentType;
                }

                _lastRequestLog = new HttpRequestLog
                {
                    Method         = method,
                    PostData       = userVariables,
                    PostBody       = postBody,
                    RequestHeaders = req.Headers,
                    Url            = uri
                };
                try
                {
                    using (IHttpWebResponse response = req.GetResponse())
                    {
                        Encoding responseEncoding = Encoding.UTF8;                         //default
                        string   charSet          = response.CharacterSet;
                        if (!String.IsNullOrEmpty(charSet))
                        {
                            try
                            {
                                responseEncoding = Encoding.GetEncoding(charSet);
                            }
                            catch (ArgumentException)
                            {
                                responseEncoding = Encoding.UTF8;                                 // try using utf8
                            }
                        }

                        StreamReader reader = new StreamReader(response.GetResponseStream(), responseEncoding);
                        html = reader.ReadToEnd();
                        responseContentType = response.ContentType;
                        reader.Close();
                        _doc = null;
                        _includeFormValues = null;

                        _lastRequestLog.Text            = html;
                        _lastRequestLog.ParsedHtml      = html;
                        _lastRequestLog.ResponseHeaders = response.Headers;
                        _lastRequestLog.StatusCode      = (int)response.StatusCode;

                        if (method == "GET" && uri.Query.Length > 0 && uri.Query != "?")
                        {
                            _lastRequestLog.QueryStringData = HttpUtility.ParseQueryString(uri.Query);
                        }
                        if ((int)response.StatusCode == 302 || (int)response.StatusCode == 301)
                        {
                            uri = new Uri(uri, response.Headers["Location"]);
                            handle301Or302Redirect = true;
                            Debug.WriteLine("Redirecting to: " + uri);
                            method        = "GET";
                            postData      = null;
                            userVariables = null;
                        }
                    }
                }
                catch (WebException ex)
                {
                    _lastRequestLog.StatusCode = (int)ex.Status.GetTypeCode();
                    if (ex.Response != null)
                    {
                        _lastRequestLog.ResponseHeaders = ex.Response.Headers;
                        StreamReader reader = new StreamReader(ex.Response.GetResponseStream());
                        html = reader.ReadToEnd();
                        _lastRequestLog.Text = html;
                    }

                    LastWebException = ex;

                    switch (ex.Status)
                    {
                    case WebExceptionStatus.Timeout:
                        Log("A timeout occurred while trying to load the web page", LogMessageType.Error);
                        break;

                    case WebExceptionStatus.ReceiveFailure:
                        Log("The response was cut short prematurely", LogMessageType.Error);
                        break;

                    default:
                        Log("An exception was thrown while trying to load the page: " + ex.Message, LogMessageType.Error);
                        break;
                    }
                    return(false);
                }
                finally
                {
                    LogRequestData();
                }
            } while (handle301Or302Redirect);
            this.RemoveChildBrowsers();             //Any frames contained in the previous state should be removed. They will be recreated if we ever navigate back
            this.AddNavigationState(new NavigationState()
            {
                Html = html, Url = uri, ContentType = contentType
            });
            return(true);
        }
        static internal IPersistentBufferProvider GetBufferProviderFromHttpRequest(IHttpWebRequest request)
        {
            if (request == null)
                return null;

            try
            {
                request.Credentials = CredentialCache.DefaultCredentials;
                request.ReadWriteTimeout = 900000;
                request.Timeout = 300000;

                using (var response = request.GetResponse())
                {
                    return GetBufferProviderFromStream(response.GetResponseStream());
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine(e);
                return null;
            }

        }
示例#17
0
        /// <summary>
        /// Gets response stream from a webrequest using the correct encoding.
        /// </summary>
        ///
        /// <param name="request">
        /// The request.
        /// </param>
        ///
        /// <returns>
        /// The response stream.
        /// </returns>

        protected StreamReader GetResponseStreamReader(IHttpWebRequest request)
        {

            var response = request.GetResponse();

            //var response = request.GetResponse();
            //var encoding = response.Headers["

            StreamReader reader;
            var encoding = GetEncoding(response);

            reader = new StreamReader(response.GetResponseStream(), encoding);
            return reader;
        }
示例#18
0
        private HttpWebHelperResult GetResponse(IHttpWebRequest httpWebRequest, Stream requestBody)
        {
            IHttpWebResponse httpWebResponse = null;
            Exception exception = null;
            HttpWebHelperResult httpWebHelperAsyncResult = null;

            try
            {
                httpWebResponse = httpWebRequest.GetResponse();
            }
            catch (WebException ex)
            {
                var webException = new WebExceptionWrapper(ex);
                httpWebResponse = webException.GetResponse();
                exception = webException;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            finally
            {
                if (httpWebResponse != null)
                {
                    var args = new ResponseReceivedEventArgs(httpWebResponse, exception, null);
                    OnResponseReceived(args);
                    httpWebHelperAsyncResult = ReadResponseStream(httpWebRequest, httpWebResponse, exception, args.ResponseSaveStream);
                }
                else
                {
                    httpWebHelperAsyncResult = new HttpWebHelperResult(httpWebRequest, httpWebResponse, exception, null, false, true, null, null);
                }
            }

            return httpWebHelperAsyncResult;
        }
示例#19
0
        /// <summary>
        /// Gets http response, returns (etag, data)
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private SolrResponse GetResponse(IHttpWebRequest request)
        {
            using (var response = request.GetResponse()) {
                var etag = response.Headers[HttpResponseHeader.ETag];
                var cacheControl = response.Headers[HttpResponseHeader.CacheControl];

                DateTime expiration = DateTime.MinValue;
                if (cacheControl != null && cacheControl.Contains("no-cache"))
                    etag = null; // avoid caching things marked as no-cache
                else if (cacheControl != null) {
                    int maxAge = 0;
                    maxAge = getMaxAge(cacheControl);
                    if (maxAge > 0) {
                        expiration = SystemTime.UtcNow().AddSeconds(maxAge);
                    }
                }
                return new SolrResponse(etag, ReadResponseToString(response), expiration);
            }
        }