Exemplo n.º 1
0
        private void FetchHtmlAsyncResponse(IAsyncResult result)
        {
            FetchHtmlState state = result.AsyncState as FetchHtmlState;

            state.IgnoreTimeout = true;

            try
            {
                AwfulDebugger.AddLog(this, AwfulDebugger.Level.Debug, "START ProcessResponse()");

                using (var response = state.Request.EndGetResponse(result))
                    using (var stream = new StreamReader(response.GetResponseStream(),
                                                         Encoding.GetEncoding(CoreConstants.WEB_RESPONSE_ENCODING)))
                    {
                        state.Html = stream.ReadToEnd();
                    }

                AwfulDebugger.AddLog(this, AwfulDebugger.Level.Debug, "END ProcessResponse()");

                if (string.IsNullOrEmpty(state.Html))
                {
                    throw new NullReferenceException("fetch html result is null or empty");
                }
            }

            catch (Exception ex) { state.Error = ex; }

            state.Callback(state);
        }
Exemplo n.º 2
0
        private void FetchHtmlStateAsync(string url, Action <FetchHtmlState> callback, int timeout = DefaultTimeoutInMilliseconds)
        {
            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Debug, string.Format("START FetchHtml({0}, {1})", url, timeout));
            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Info, "Performing authentication check...");

            Authenticate();

            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Info, "Authentication check complete.");
            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Info, string.Format("Requesting html from url '{0}'...", url));

            if (!NetworkInterface.GetIsNetworkAvailable())
            {
                throw new Exception("The network is unavailable. Check your network settings and please try again.");
            }

            if (SimulateTimeout)
            {
                AwfulDebugger.AddLog(this, AwfulDebugger.Level.Info, "SimulateTimeout = true.");
                Random random = new Random();
                int    value  = random.Next(1, 101);
                if (value <= SimulateTimeoutChance)
                {
                    AwfulDebugger.AddLog(this, AwfulDebugger.Level.Info, "Timeout generated.");
                    throw new TimeoutException("Artificial timeout generated!");
                }
            }

            FetchHtmlState state = new FetchHtmlState(timeout);

            state.Callback = callback;
            state.Request  = AwfulWebRequest.CreateGetRequest(url);
            state.Request.BeginGetResponse(FetchHtmlAsyncResponse, state);
            state.StartTimer();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Fetches raw html from the specified url.
        /// </summary>
        /// <param name="url">The location of the target html.</param>
        /// <param name="timeout">The timeout value, in milliseconds, before cancelling the request.
        /// If no value is supplied, a default value of 10 seconds is used.</param>
        /// <returns>A string representing the raw html.</returns>
        public string FetchHtml(string url, int timeout = DefaultTimeoutInMilliseconds)
        {
            AutoResetEvent signal = new AutoResetEvent(false);
            FetchHtmlState state  = null;

            FetchHtmlStateAsync(url, callback =>
            {
                state = callback;
                signal.Set();
            }, timeout);


            signal.WaitOne();

            if (state.Error != null)
            {
                throw state.Error;
            }

            return(state.Html);
        }
Exemplo n.º 4
0
        private void FetchHtmlStateAsync(string url, Action<FetchHtmlState> callback, int timeout = DefaultTimeoutInMilliseconds)
        {
            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Debug, string.Format("START FetchHtml({0}, {1})", url, timeout));
            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Info, "Performing authentication check...");

            Authenticate();

            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Info, "Authentication check complete.");
            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Info, string.Format("Requesting html from url '{0}'...", url));

            if (!NetworkInterface.GetIsNetworkAvailable())
                throw new Exception("The network is unavailable. Check your network settings and please try again.");

            if (SimulateTimeout)
            {
                AwfulDebugger.AddLog(this, AwfulDebugger.Level.Info, "SimulateTimeout = true.");
                Random random = new Random();
                int value = random.Next(1, 101);
                if (value <= SimulateTimeoutChance)
                {
                    AwfulDebugger.AddLog(this, AwfulDebugger.Level.Info, "Timeout generated.");
                    throw new TimeoutException("Artificial timeout generated!");
                }
            }

            FetchHtmlState state = new FetchHtmlState(timeout);
            state.Callback = callback;
            state.Request = AwfulWebRequest.CreateGetRequest(url);
            state.Request.BeginGetResponse(FetchHtmlAsyncResponse, state);
            state.StartTimer();
        }