コード例 #1
0
        public HttpRequestResult Get(Uri url)
        {
            HttpRequestResult results = (from httpRequestResult in HttpRequestResults
                                         where httpRequestResult.RequestUrl.Equals(url)
                                         select httpRequestResult).FirstOrDefault();

            if (results == null)
            {
                results             = new HttpRequestResult();
                results.RequestUrl  = url;
                results.Start       = DateTime.Now;
                results.BrowserUsed = this;

                StreamReader streamReader = null;
                WebResponse  response     = null;
                try
                {
                    var request = (HttpWebRequest)WebRequest.Create(url);
                    request.MaximumAutomaticRedirections = MaximumAutomaticRedirections;
                    request.AllowAutoRedirect            = AllowAutoRedirect;
                    request.UserAgent = UserAgent;
                    request.Accept    = Accept;
                    request.Headers.Add("Accept-Charset", AcceptCharset);
                    request.Headers.Add("Accept-Language", AcceptLanguage);
                    request.Credentials = this.Credentials;

                    response     = request.GetResponse();
                    streamReader = new StreamReader(response.GetResponseStream());

                    string content = streamReader.ReadToEnd();

                    results.ContentType = response.ContentType;
                    if (results.IsCss || results.IsHtml)
                    {
                        results.Content = content;
                    }
                    results.ResultUrl = request.Address;

                    HttpRequestResults.Add(results);
                }
                catch (WebException exception)
                {
                    var error = new HttpValidationError()
                    {
                        AbsoluteUri = url,
                        Error       = exception,
                        Message     = exception.Message
                    };

                    if (exception.Status == WebExceptionStatus.ProtocolError)
                    {
                        error.HttpCode = (int)((HttpWebResponse)exception.Response).StatusCode;
                    }

                    results.Error = error;
                }
                catch (Exception exception)
                {
                    results.Error = new HttpValidationError()
                    {
                        AbsoluteUri = url,
                        Error       = exception,
                        Message     = exception.Message
                    };
                }
                finally
                {
                    results.End = DateTime.Now;
                    if (streamReader != null)
                    {
                        try { streamReader.Close(); }
                        catch { }
                    }
                    if (response != null)
                    {
                        try { response.Close(); }
                        catch { }
                    }
                }
            }

            return(results);
        }
コード例 #2
0
        public HttpRequestResult Get(Uri url)
        {
            HttpRequestResult results = (from httpRequestResult in HttpRequestResults
                            where httpRequestResult.RequestUrl.Equals(url)
                            select httpRequestResult).FirstOrDefault();

            if (results == null)
            {
                results = new HttpRequestResult();
                results.RequestUrl = url;
                results.Start = DateTime.Now;
                results.BrowserUsed = this;

                StreamReader streamReader = null;
                WebResponse response = null;
                try
                {
                    var request = (HttpWebRequest)WebRequest.Create(url);
                    request.MaximumAutomaticRedirections = MaximumAutomaticRedirections;
                    request.AllowAutoRedirect = AllowAutoRedirect;
                    request.UserAgent = UserAgent;
                    request.Accept = Accept;
                    request.Headers.Add("Accept-Charset", AcceptCharset);
                    request.Headers.Add("Accept-Language", AcceptLanguage);
                    request.Credentials = this.Credentials;

                    response = request.GetResponse();
                    streamReader = new StreamReader(response.GetResponseStream());

                    string content = streamReader.ReadToEnd();

                    results.ContentType = response.ContentType;
                    if (results.IsCss || results.IsHtml)
                    {
                        results.Content = content;
                    }
                    results.ResultUrl = request.Address;

                    HttpRequestResults.Add(results);
                }
                catch (WebException exception)
                {
                    var error = new HttpValidationError()
                    {
                        AbsoluteUri = url,
                        Error = exception,
                        Message = exception.Message
                    };

                    if (exception.Status == WebExceptionStatus.ProtocolError)
                        error.HttpCode = (int)((HttpWebResponse)exception.Response).StatusCode;

                    results.Error = error;
                }
                catch (Exception exception)
                {
                    results.Error = new HttpValidationError()
                        {
                            AbsoluteUri = url,
                            Error = exception,
                            Message = exception.Message
                        };
                }
                finally
                {
                    results.End = DateTime.Now;
                    if (streamReader != null)
                    {
                        try { streamReader.Close(); }
                        catch { }
                    }
                    if (response != null)
                    {
                        try { response.Close(); }
                        catch { }
                    }
                }
            }

            return results;
        }