Exemple #1
0
        /// <summary>
        /// Loads this <see cref="MyCustomRssFeed"/> instance asynchronously using the specified <see cref="Uri"/>, <see cref="SyndicationResourceLoadSettings"/>, <see cref="ICredentials"/>, and <see cref="IWebProxy"/>.
        /// </summary>
        /// <param name="source">A <see cref="Uri"/> that represents the URL of the syndication resource XML data.</param>
        /// <param name="settings">The <see cref="SyndicationResourceLoadSettings"/> object used to configure the <see cref="MyCustomRssFeed"/> instance. This value can be <b>null</b>.</param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
        /// <param name="userToken">A user-defined object that is passed to the method invoked when the asynchronous operation completes.</param>
        /// <remarks>
        ///     <para>
        ///         To receive notification when the operation has completed or the operation has been canceled, add an event handler to the <see cref="Loaded"/> event.
        ///         You can cancel a <see cref="LoadAsync(Uri, SyndicationResourceLoadSettings, ICredentials, IWebProxy, Object)"/> operation by calling the <see cref="LoadAsyncCancel()"/> method.
        ///     </para>
        ///     <para>
        ///         After calling <see cref="LoadAsync(Uri, SyndicationResourceLoadSettings, ICredentials, IWebProxy, Object)"/>,
        ///         you must wait for the load operation to complete before attempting to load the syndication resource using the <see cref="LoadAsync(Uri, Object)"/> method.
        ///     </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="FormatException">The <paramref name="source"/> data does not conform to the expected syndication content format. In this case, the feed remains empty.</exception>
        /// <exception cref="InvalidOperationException">This <see cref="MyCustomRssFeed"/> has a <see cref="LoadAsync(Uri, SyndicationResourceLoadSettings, ICredentials, IWebProxy, Object)"/> call in progress.</exception>
        public void LoadAsync(Uri source, SyndicationResourceLoadSettings settings, WebRequestOptions options, Object userToken)
        {
            Guard.ArgumentNotNull(source, "source");
            if (settings == null)
            {
                settings = new SyndicationResourceLoadSettings();
            }
            if (this.LoadOperationInProgress)
            {
                throw new InvalidOperationException();
            }
            this.LoadOperationInProgress   = true;
            this.AsyncLoadHasBeenCancelled = false;


            asyncHttpWebRequest         = SyndicationEncodingUtility.CreateWebRequest(source, options);
            asyncHttpWebRequest.Timeout = Convert.ToInt32(settings.Timeout.TotalMilliseconds, System.Globalization.NumberFormatInfo.InvariantInfo);


            object[] state = new object[6] {
                asyncHttpWebRequest, this, source, settings, options, userToken
            };
            IAsyncResult result = asyncHttpWebRequest.BeginGetResponse(new AsyncCallback(AsyncLoadCallback), state);

            ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(AsyncTimeoutCallback), state, settings.Timeout, true);
        }
        /// <summary>
        /// Called when a corresponding asynchronous send operation completes.
        /// </summary>
        /// <param name="result">The result of the asynchronous operation.</param>
        private static void AsyncSendCallback(IAsyncResult result)
        {
            XmlRpcResponse    response       = null;
            WebRequest        httpWebRequest = null;
            XmlRpcClient      client         = null;
            Uri               host           = null;
            XmlRpcMessage     message        = null;
            WebRequestOptions options        = null;
            object            userToken      = null;

            if (result.IsCompleted)
            {
                object[] parameters = (object[])result.AsyncState;
                httpWebRequest = parameters[0] as WebRequest;
                client         = parameters[1] as XmlRpcClient;
                host           = parameters[2] as Uri;
                message        = parameters[3] as XmlRpcMessage;
                options        = parameters[4] as WebRequestOptions;
                userToken      = parameters[5];

                if (client != null)
                {
                    WebResponse httpWebResponse = (WebResponse)httpWebRequest.EndGetResponse(result);

                    response = new XmlRpcResponse(httpWebResponse);

                    client.OnMessageSent(new XmlRpcMessageSentEventArgs(host, message, response, options, userToken));

                    client.SendOperationInProgress = false;
                }
            }
        }
Exemple #3
0
        private static HttpRequestMessage CreateRequestInternal(LazyUri url, WebRequestOptions options, bool forceSameUrl, int redirectionIndex
#if WEBCLIENT
                                                                , out HttpContent requestContent
#endif
                                                                )
        {
            if (!forceSameUrl)
            {
                url = MaybeAddAdditionalQueryParameters(url, options);
            }

            var pathAndQueryConsistentUrl = url.GetPathAndQueryConsistentUrlIfCached();

            if (pathAndQueryConsistentUrl == null)
            {
                if (
#if NET35
                    url.FragmentParameters.Count()
#else
                    url.FragmentParameters.Count
#endif
                    >= Configuration_MinNumberOfFragmentParametersForAdHocRequestUri)
                {
                    pathAndQueryConsistentUrl = url.GetPathAndQueryAsUri();
                }
                else
                {
                    pathAndQueryConsistentUrl = url.PathAndQueryConsistentUrl;
                }
            }
Exemple #4
0
        public async Task <HttpResponseMessage> GetResponseAsync(long startPosition)
        {
            if (partialDownload != null && startPosition == 0)
            {
                var c = partialDownload;
                partialDownload = null;
                return(c);
            }
            var options = new WebRequestOptions()
            {
                Timeout = 30000,
                TimeoutSecondRetrialAfterError = 10000,
                TimeoutStartSecondRetrial      = null
            };
            var url = new LazyUri(this.Url);

            HttpExtensionMethods.ProcessMetaParameters(url, options);
            if (startPosition != 0)
            {
                options.AddHeader("Range", "bytes=" + startPosition + "-");
            }
            var response = await HttpExtensionMethods.GetResponseAsync(url, options);

            SaveResponseInfo(response, null);

            return(response);
            //return (await HttpExtensionMethods.SendAsync(url, options, null)).Response;
        }
Exemple #5
0
        internal HtmlNode RecreateNode(LazyUri url, WebRequestOptions cookieDestination, string cachePath)
        {
            if (this.ExceptionType != null)
            {
                throw Caching.RebuildException(this, url);
            }
            if (this.RedirectUrl != null && this.Result == null)
            {
                return(null);
            }
            HtmlNode html;

            if (this.DataType == WebCacheDataType.Json)
            {
                html = FizzlerCustomSelectors.JsonToHtml(this.Result, 0, null);
            }
            else if (this.DataType == WebCacheDataType.Text)
            {
                var d = new HtmlDocument();
                html = d.DocumentNode;
                html.SetAttributeValue("plain-text", "1");
                html.AppendTextNode(this.Result);
            }
            else
            {
                html = this.Result.AsHtmlDocumentNode();
            }
            var docnode = html.OwnerDocument.DocumentNode;

            docnode.SetAttributeValue("from-cache", "1");
            if (this.Headers != null)
            {
                foreach (var header in this.Headers)
                {
                    docnode.SetAttributeValue("header-" + header.Key, header.Value);
                }
            }
            if (this.Cookies != null)
            {
                foreach (var cookie in this.Cookies)
                {
                    cookieDestination.AddCookie(cookie.Key, cookie.Value, PriorityCookie.PRIORITY_FromCache);
                }
            }

#if DESKTOP
            if (this.DateRetrieved == default(DateTime))
            {
                this.DateRetrieved = File.GetLastWriteTimeUtc(cachePath);
            }
#endif
            docnode.SetAttributeValue("date-retrieved", this.DateRetrieved.ToString("o"));
            if (RedirectUrl != null)
            {
                docnode.SetAttributeValue("redirect-url", this.RedirectUrl.AbsoluteUri);
            }
            docnode.SetAttributeValue("requested-url", url.AbsoluteUri);
            html.OwnerDocument.SetPageUrl(this.PageUrl ?? this.Url);
            return(html);
        }
Exemple #6
0
        /// <summary>
        /// Called when a corresponding asynchronous load operation completes.
        /// </summary>
        /// <param name="result">The result of the asynchronous operation.</param>
        private static void AsyncLoadCallback(IAsyncResult result)
        {
            System.Text.Encoding encoding       = System.Text.Encoding.UTF8;
            XPathNavigator       navigator      = null;
            WebRequest           httpWebRequest = null;
            MyCustomRssFeed      feed           = null;
            Uri source = null;
            WebRequestOptions options = null;
            SyndicationResourceLoadSettings settings = null;

            if (result.IsCompleted)
            {
                object[] parameters = (object[])result.AsyncState;
                httpWebRequest = parameters[0] as WebRequest;
                feed           = parameters[1] as MyCustomRssFeed;
                source         = parameters[2] as Uri;
                settings       = parameters[3] as SyndicationResourceLoadSettings;
                options        = parameters[4] as WebRequestOptions;
                object userToken = parameters[5];
                if (feed != null)
                {
                    WebResponse httpWebResponse = (WebResponse)httpWebRequest.EndGetResponse(result);
                    using (Stream stream = httpWebResponse.GetResponseStream())
                    {
                        if (settings != null)
                        {
                            encoding = settings.CharacterEncoding;
                        }

                        using (StreamReader streamReader = new StreamReader(stream, encoding))
                        {
                            XmlReaderSettings readerSettings = new XmlReaderSettings();
                            readerSettings.IgnoreComments   = true;
                            readerSettings.IgnoreWhitespace = true;
                            readerSettings.DtdProcessing    = DtdProcessing.Ignore;

                            using (XmlReader reader = XmlReader.Create(streamReader, readerSettings))
                            {
                                if (encoding == System.Text.Encoding.UTF8)
                                {
                                    navigator = SyndicationEncodingUtility.CreateSafeNavigator(source, options, null);
                                }
                                else
                                {
                                    navigator = SyndicationEncodingUtility.CreateSafeNavigator(source, options, settings.CharacterEncoding);
                                }

                                //  Code to load the syndication resource using the XPathNavigator would go here.
                                //  If you support legacy formats, you would use a SyndicationResourceAdapter to fill the feed;
                                //  otherwise you would utilize the feed's Load method.
                                feed.OnFeedLoaded(new SyndicationResourceLoadedEventArgs(navigator, source, options, userToken));
                            }
                        }
                    }
                    feed.LoadOperationInProgress = false;
                }
            }
        }
        //============================================================
        //	CALLBACK DELEGATE METHODS
        //============================================================
        #region AsyncSendCallback(IAsyncResult result)
        /// <summary>
        /// Called when a corresponding asynchronous send operation completes.
        /// </summary>
        /// <param name="result">The result of the asynchronous operation.</param>
        private static void AsyncSendCallback(IAsyncResult result)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            TrackbackResponse response       = null;
            WebRequest        httpWebRequest = null;
            TrackbackClient   client         = null;
            Uri host = null;
            TrackbackMessage  message   = null;
            WebRequestOptions options   = null;
            object            userToken = null;

            //------------------------------------------------------------
            //	Determine if the async send operation completed
            //------------------------------------------------------------
            if (result.IsCompleted)
            {
                //------------------------------------------------------------
                //	Extract the send operations parameters from the user state
                //------------------------------------------------------------
                object[] parameters = (object[])result.AsyncState;
                httpWebRequest = parameters[0] as WebRequest;
                client         = parameters[1] as TrackbackClient;
                host           = parameters[2] as Uri;
                message        = parameters[3] as TrackbackMessage;
                options        = parameters[4] as WebRequestOptions;
                userToken      = parameters[5];

                //------------------------------------------------------------
                //	Verify expected parameters were found
                //------------------------------------------------------------
                if (client != null)
                {
                    //------------------------------------------------------------
                    //	Get the Trackback response to the Trackback ping request
                    //------------------------------------------------------------
                    WebResponse httpWebResponse = (WebResponse)httpWebRequest.EndGetResponse(result);

                    //------------------------------------------------------------
                    //	Extract the Trackback response to the Trackback ping request
                    //------------------------------------------------------------
                    response = new TrackbackResponse(httpWebResponse);

                    //------------------------------------------------------------
                    //	Raise SendCompleted event to notify registered handlers of state change
                    //------------------------------------------------------------
                    client.OnMessageSent(new TrackbackMessageSentEventArgs(host, message, response, options, userToken));

                    //------------------------------------------------------------
                    //	Reset async operation in progress indicator
                    //------------------------------------------------------------
                    client.SendOperationInProgress = false;
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="XmlRpcMessageSentEventArgs"/> class using the supplied parameters.
        /// </summary>
        /// <param name="host">A <see cref="Uri"/> that represents the URL of the host computer used for the XML-RPC transaction.</param>
        /// <param name="message">An <see cref="XmlRpcMessage"/> that represents the remote procedure call payload.</param>
        /// <param name="response">An <see cref="XmlRpcResponse"/> that represents the response to the remote procedure call.</param>
        /// <param name="credentials">A <see cref="ICredentials"/> that represents the authentication credentials utilized by the client when making the remote procedure call. This parameter may be <b>null</b>.</param>
        /// <param name="proxy">A <see cref="IWebProxy"/> that represents the web proxy utilized by the client to proxy the remote procedure call. This parameter may be <b>null</b>.</param>
        /// <param name="state">A <see cref="Object"/> containing state information that was passed to the asynchronous send operation. This parameter may be <b>null</b>.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="host"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="message"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="response"/> is a null reference (Nothing in Visual Basic).</exception>
        public XmlRpcMessageSentEventArgs(Uri host, XmlRpcMessage message, XmlRpcResponse response, ICredentials credentials, IWebProxy proxy, Object state)
        {
            Guard.ArgumentNotNull(host, "host");
            Guard.ArgumentNotNull(message, "message");
            Guard.ArgumentNotNull(response, "response");

            eventHost      = host;
            eventMessage   = message;
            eventResponse  = response;
            eventOptions   = new WebRequestOptions(credentials, proxy);
            eventUserToken = state;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="XmlRpcMessageSentEventArgs"/> class using the supplied parameters.
        /// </summary>
        /// <param name="host">A <see cref="Uri"/> that represents the URL of the host computer used for the XML-RPC transaction.</param>
        /// <param name="message">An <see cref="XmlRpcMessage"/> that represents the remote procedure call payload.</param>
        /// <param name="response">An <see cref="XmlRpcResponse"/> that represents the response to the remote procedure call.</param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
        /// <param name="state">A <see cref="Object"/> containing state information that was passed to the asynchronous send operation. This parameter may be <b>null</b>.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="host"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="message"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="response"/> is a null reference (Nothing in Visual Basic).</exception>
        public XmlRpcMessageSentEventArgs(Uri host, XmlRpcMessage message, XmlRpcResponse response, WebRequestOptions options, Object state)
        {
            Guard.ArgumentNotNull(host, "host");
            Guard.ArgumentNotNull(message, "message");
            Guard.ArgumentNotNull(response, "response");

            eventHost      = host;
            eventMessage   = message;
            eventResponse  = response;
            eventOptions   = options ?? new WebRequestOptions();
            eventUserToken = state;
        }
        private static WebRequestOptions ProcessWebRequestOptions(dynamic options)
        {
            var webRequestOptions = new WebRequestOptions()
            {
                Retries = DefaultServiceRetries,
                Timeout = DefaultTimeout * 1000
            };

            // Decode options
            if (options != null)
            {
                var o = (object)options;
                IDictionary <string, object> convertedValue = o.FromScriptValue() as IDictionary <string, object>;

                if (convertedValue.ContainsKey("Retries"))
                {
                    webRequestOptions.Retries = (int)convertedValue["Retries"];
                }

                if (convertedValue.ContainsKey("Timeout"))
                {
                    webRequestOptions.Timeout = (int)convertedValue["Timeout"] * 1000;
                }

                if (convertedValue.ContainsKey("Headers"))
                {
                    webRequestOptions.Headers = convertedValue["Headers"] as Dictionary <string, object>;
                }

                if (convertedValue.ContainsKey("KeepResponseAsString"))
                {
                    webRequestOptions.KeepResponseAsString = Convert.ToBoolean(convertedValue["KeepResponseAsString"]);
                }

                if (convertedValue.ContainsKey("KeepRequestAsString"))
                {
                    webRequestOptions.KeepRequestAsString = Convert.ToBoolean(convertedValue["KeepRequestAsString"]);
                }
            }

            if (webRequestOptions.Timeout > MaxTimeout)
            {
                logger.WarnFormat("Script requesting timeout of {0} which is greater than max allowed value of {1}", webRequestOptions.Timeout, MaxTimeout);
                webRequestOptions.Timeout = MaxTimeout;
            }

            return(webRequestOptions);
        }
Exemple #11
0
        internal static HtmlNode TryReadFromCache(string cachePath, LazyUri url, WebRequestOptions cookieDestination)
        {
            var data = Caching.TryReadCacheFile(cachePath);

            if (data == null)
            {
                return(null);
            }
            if (data.ExceptionType == null || !Caching.IgnoreCachedFailedRequests)
            {
                Utils.RaiseWebRequestEvent(url, true);
                return(data.RecreateNode(url, cookieDestination, cachePath));
            }

            return(null);
        }
        /// <summary>
        /// Loads the syndication resource from the supplied <see cref="Uri"/> using the specified <see cref="ICredentials">credentials</see>, <see cref="IWebProxy">proxy</see> and <see cref="SyndicationResourceLoadSettings"/>.
        /// </summary>
        /// <param name="source">A <see cref="Uri"/> that points to the location of the web resource used to load the syndication resource.</param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
        /// <param name="settings">The <see cref="SyndicationResourceLoadSettings"/> object used to configure the <see cref="AtomEntry"/> instance. This value can be <b>null</b>.</param>
        /// <remarks>
        ///     <para>
        ///         <list type="bullet">
        ///             <item>
        ///                 <description>
        ///                     If <paramref name="settings"/> has a <see cref="SyndicationResourceLoadSettings.CharacterEncoding">character encoding</see> of <see cref="System.Text.Encoding.UTF8"/>
        ///                     the character encoding of the <paramref name="source"/> will be attempt to be determined automatically, otherwise the specified character encoding will be used.
        ///                     If automatic detection fails, a character encoding of <see cref="System.Text.Encoding.UTF8"/> is used by default.
        ///                 </description>
        ///             </item>
        ///             <item>
        ///                 <description>
        ///                     After the load operation has successfully completed, the <see cref="AtomEntry.Loaded"/> event will be raised.
        ///                 </description>
        ///             </item>
        ///         </list>
        ///     </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="FormatException">The <paramref name="source"/> data does not conform to the expected syndication content format. In this case, the entry remains empty.</exception>
        /// <exception cref="XmlException">There is a load or parse error in the XML. In this case, the entry remains empty.</exception>
        public new void Load(Uri source, WebRequestOptions options, SyndicationResourceLoadSettings settings)
        {
            base.Load(source, options, settings);

            AtomPublishingEditedSyndicationExtension editedExtension = this.FindExtension(AtomPublishingEditedSyndicationExtension.MatchByType) as AtomPublishingEditedSyndicationExtension;

            if (editedExtension != null)
            {
                this.EditedOn = editedExtension.Context.EditedOn;
            }

            AtomPublishingControlSyndicationExtension controlExtension = this.FindExtension(AtomPublishingControlSyndicationExtension.MatchByType) as AtomPublishingControlSyndicationExtension;

            if (controlExtension != null)
            {
                this.IsDraft = controlExtension.Context.IsDraft;
            }
        }
Exemple #13
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TrackbackMessageSentEventArgs"/> class using the supplied parameters.
        /// </summary>
        /// <param name="host">A <see cref="Uri"/> that represents the URL of the host computer used for the XML-RPC transaction.</param>
        /// <param name="message">An <see cref="TrackbackMessage"/> that represents the Trackback ping request payload.</param>
        /// <param name="response">An <see cref="TrackbackResponse"/> that represents the response to the Trackback ping request.</param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
        /// <param name="state">A <see cref="Object"/> containing state information that was passed to the asynchronous send operation. This parameter may be <b>null</b>.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="host"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="message"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="response"/> is a null reference (Nothing in Visual Basic).</exception>
        public TrackbackMessageSentEventArgs(Uri host, TrackbackMessage message, TrackbackResponse response, WebRequestOptions options, Object state)
        {
            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(host, "host");
            Guard.ArgumentNotNull(message, "message");
            Guard.ArgumentNotNull(response, "response");

            //------------------------------------------------------------
            //	Initialize class members
            //------------------------------------------------------------
            eventHost      = host;
            eventMessage   = message;
            eventResponse  = response;
            eventOptions   = options ?? new WebRequestOptions();
            eventUserToken = state;
        }
Exemple #14
0
        private static LazyUri MaybeAddAdditionalQueryParameters(LazyUri url, WebRequestOptions options)
        {
            bool cloned = false;

            if (options != null && options.AdditionalQueryParameters != null)
            {
                foreach (var item in Flatten(options.AdditionalQueryParameters))
                {
                    if (!cloned)
                    {
                        cloned = true;
                        url    = url.Clone();
                    }
                    url.AppendQueryParameter(item.Key, ToString(item.Value));
                }
            }
            return(url);
        }
Exemple #15
0
        /// <summary>
        /// Initializes a new instance of the <see cref="XmlRpcMessageSentEventArgs"/> class using the supplied parameters.
        /// </summary>
        /// <param name="host">A <see cref="Uri"/> that represents the URL of the host computer used for the XML-RPC transaction.</param>
        /// <param name="message">An <see cref="XmlRpcMessage"/> that represents the remote procedure call payload.</param>
        /// <param name="response">An <see cref="XmlRpcResponse"/> that represents the response to the remote procedure call.</param>
        /// <param name="credentials">A <see cref="ICredentials"/> that represents the authentication credentials utilized by the client when making the remote procedure call. This parameter may be <b>null</b>.</param>
        /// <param name="proxy">A <see cref="IWebProxy"/> that represents the web proxy utilized by the client to proxy the remote procedure call. This parameter may be <b>null</b>.</param>
        /// <param name="state">A <see cref="Object"/> containing state information that was passed to the asynchronous send operation. This parameter may be <b>null</b>.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="host"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="message"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="response"/> is a null reference (Nothing in Visual Basic).</exception>
        public XmlRpcMessageSentEventArgs(Uri host, XmlRpcMessage message, XmlRpcResponse response, ICredentials credentials, IWebProxy proxy, Object state)
        {
            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(host, "host");
            Guard.ArgumentNotNull(message, "message");
            Guard.ArgumentNotNull(response, "response");

            //------------------------------------------------------------
            //	Initialize class members
            //------------------------------------------------------------
            eventHost      = host;
            eventMessage   = message;
            eventResponse  = response;
            eventOptions   = new WebRequestOptions(credentials, proxy);
            eventUserToken = state;
        }
Exemple #16
0
        internal HtmlNode RecreateNode(LazyUri url, WebRequestOptions cookieDestination, string cachePath)
        {
            if (this.ExceptionType != null) throw Caching.RebuildException(this, url);
            if (this.RedirectUrl != null && this.Result == null) return null;
            HtmlNode html;
            if (this.DataType == WebCacheDataType.Json) html = FizzlerCustomSelectors.JsonToHtml(this.Result, 0, null);
            else if (this.DataType == WebCacheDataType.Text)
            {
                var d = new HtmlDocument();
                html = d.DocumentNode;
                html.SetAttributeValue("plain-text", "1");
                html.AppendTextNode(this.Result);
            }
            else
            {
                html = this.Result.AsHtmlDocumentNode();
            }
            var docnode = html.OwnerDocument.DocumentNode;
            docnode.SetAttributeValue("from-cache", "1");
            if (this.Headers != null)
            {
                foreach (var header in this.Headers)
                {
                    docnode.SetAttributeValue("header-" + header.Key, header.Value);
                }
            }
            if (this.Cookies != null)
            {
                foreach (var cookie in this.Cookies)
                {
                    cookieDestination.AddCookie(cookie.Key, cookie.Value, PriorityCookie.PRIORITY_FromCache);
                }
            }

#if DESKTOP
            if (this.DateRetrieved == default(DateTime)) this.DateRetrieved = File.GetLastWriteTimeUtc(cachePath);
#endif
            docnode.SetAttributeValue("date-retrieved", this.DateRetrieved.ToString("o"));
            if (RedirectUrl != null)
                docnode.SetAttributeValue("redirect-url", this.RedirectUrl.AbsoluteUri);
            docnode.SetAttributeValue("requested-url", url.AbsoluteUri);
            html.OwnerDocument.SetPageUrl(this.PageUrl ?? this.Url);
            return html;
        }
Exemple #17
0
        /// <summary>
        /// Loads the syndication resource from the supplied <see cref="Uri"/> using the specified <see cref="ICredentials">credentials</see>, <see cref="IWebProxy">proxy</see> and <see cref="SyndicationResourceLoadSettings"/>.
        /// </summary>
        /// <param name="source">A <see cref="Uri"/> that points to the location of the web resource used to load the syndication resource.</param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
        /// <param name="settings">The <see cref="SyndicationResourceLoadSettings"/> object used to configure the <see cref="MyCustomRssFeed"/> instance. This value can be <b>null</b>.</param>
        /// <remarks>
        ///     <para>
        ///         <list type="bullet">
        ///             <item>
        ///                 <description>
        ///                     If <paramref name="settings"/> has a <see cref="SyndicationResourceLoadSettings.CharacterEncoding">character encoding</see> of <see cref="System.Text.Encoding.UTF8"/>
        ///                     the character encoding of the <paramref name="source"/> will be attempt to be determined automatically, otherwise the specified character encoding will be used.
        ///                     If automatic detection fails, a character encoding of <see cref="System.Text.Encoding.UTF8"/> is used by default.
        ///                 </description>
        ///             </item>
        ///             <item>
        ///                 <description>
        ///                     After the load operation has successfully completed, the <see cref="MyCustomRssFeed.Loaded"/> event will be raised.
        ///                 </description>
        ///             </item>
        ///         </list>
        ///     </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="FormatException">The <paramref name="source"/> data does not conform to the expected syndication content format. In this case, the feed remains empty.</exception>
        /// <exception cref="XmlException">There is a load or parse error in the XML. In this case, the feed remains empty.</exception>
        public void Load(Uri source, WebRequestOptions options, SyndicationResourceLoadSettings settings)
        {
            XPathNavigator navigator = null;

            Guard.ArgumentNotNull(source, "source");
            if (settings == null)
            {
                settings = new SyndicationResourceLoadSettings();
            }
            if (settings.CharacterEncoding == System.Text.Encoding.UTF8)
            {
                navigator = SyndicationEncodingUtility.CreateSafeNavigator(source, options, null);
            }
            else
            {
                navigator = SyndicationEncodingUtility.CreateSafeNavigator(source, options, settings.CharacterEncoding);
            }
            this.Load(navigator, settings, new SyndicationResourceLoadedEventArgs(navigator, source, options));
        }
Exemple #18
0
        internal static async Task <HttpResponseMessage> GetAzureResponseAsync(string container, string name, long startPosition, Shaman.Types.WebFile file)
        {
            var options = new WebRequestOptions()
            {
                Timeout = 30000, TimeoutSecondRetrialAfterError = 10000, TimeoutStartSecondRetrial = null
            };

            if (startPosition != 0)
            {
                options.AddHeader("Range", "bytes=" + startPosition + "-");
            }
            var response = (await ExtensionMethods.SendAsync(new LazyUri(AzureApi.GetUrlForBlob(container, name)), options, null)).Response;
            IEnumerable <string> errs;

            if (response.Headers.TryGetValues("x-ms-meta-err", out errs))
            {
                throw MediaStream.ExceptionFromCachedResponse(errs.First());
            }

            return(response);
        }
 public Task PopulateRequestsAsync()
 {
     return this.Requests.Where(x => x.RequestBody == null).ForEachThrottledAsync(async x =>
     {
         try
         {
             var req = new WebRequestOptions();
             foreach (var header in x.RequestHeaders)
             {
                 req.AddHeader(header.Key, header.Value);
             }
             using (var resp = await new LazyUri(x.Url).GetResponseAsync(req))
             {
                 using (var sr = new StreamReader(await resp.Content.ReadAsStreamAsync()))
                 {
                     x.ResponseBody = await sr.ReadToEndAsync();
                 }
             }
         }
         catch (Exception)
         {
         }
     }, 1);
 }
        /// <summary>
        /// Loads the syndication resource from the supplied <see cref="Uri"/> using the specified <see cref="ICredentials">credentials</see>, <see cref="IWebProxy">proxy</see> and <see cref="SyndicationResourceLoadSettings"/>.
        /// </summary>
        /// <param name="source">A <see cref="Uri"/> that points to the location of the web resource used to load the syndication resource.</param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
        /// <param name="settings">The <see cref="SyndicationResourceLoadSettings"/> object used to configure the <see cref="AtomEntry"/> instance. This value can be <b>null</b>.</param>
        /// <remarks>
        ///     <para>
        ///         <list type="bullet">
        ///             <item>
        ///                 <description>
        ///                     If <paramref name="settings"/> has a <see cref="SyndicationResourceLoadSettings.CharacterEncoding">character encoding</see> of <see cref="System.Text.Encoding.UTF8"/>
        ///                     the character encoding of the <paramref name="source"/> will be attempt to be determined automatically, otherwise the specified character encoding will be used.
        ///                     If automatic detection fails, a character encoding of <see cref="System.Text.Encoding.UTF8"/> is used by default.
        ///                 </description>
        ///             </item>
        ///             <item>
        ///                 <description>
        ///                     After the load operation has successfully completed, the <see cref="AtomEntry.Loaded"/> event will be raised.
        ///                 </description>
        ///             </item>
        ///         </list>
        ///     </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="FormatException">The <paramref name="source"/> data does not conform to the expected syndication content format. In this case, the entry remains empty.</exception>
        /// <exception cref="XmlException">There is a load or parse error in the XML. In this case, the entry remains empty.</exception>
        public new void Load(Uri source, WebRequestOptions options, SyndicationResourceLoadSettings settings)
        {
            //------------------------------------------------------------
            //	Load syndication resource using base entry implementation
            //------------------------------------------------------------
            base.Load(source, options, settings);

            //------------------------------------------------------------
            //	Extract member resource information from extensions
            //------------------------------------------------------------
            AtomPublishingEditedSyndicationExtension editedExtension = this.FindExtension(AtomPublishingEditedSyndicationExtension.MatchByType) as AtomPublishingEditedSyndicationExtension;

            if (editedExtension != null)
            {
                this.EditedOn = editedExtension.Context.EditedOn;
            }

            AtomPublishingControlSyndicationExtension controlExtension = this.FindExtension(AtomPublishingControlSyndicationExtension.MatchByType) as AtomPublishingControlSyndicationExtension;

            if (controlExtension != null)
            {
                this.IsDraft = controlExtension.Context.IsDraft;
            }
        }
Exemple #21
0
 public Task PopulateRequestsAsync()
 {
     return(this.Requests.Where(x => x.RequestBody == null).ForEachThrottledAsync(async x =>
     {
         try
         {
             var req = new WebRequestOptions();
             foreach (var header in x.RequestHeaders)
             {
                 req.AddHeader(header.Key, header.Value);
             }
             using (var resp = await new LazyUri(x.Url).GetResponseAsync(req))
             {
                 using (var sr = new StreamReader(await resp.Content.ReadAsStreamAsync()))
                 {
                     x.ResponseBody = await sr.ReadToEndAsync();
                 }
             }
         }
         catch (Exception)
         {
         }
     }, 1));
 }
        /// <summary>
        /// Initializes a new <see cref="WebRequest"/> suitable for sending a remote procedure call using the supplied host, user agent, message, credentials, and proxy.
        /// </summary>
        /// <param name="host">A <see cref="Uri"/> that represents the URL of the host computer used for XML-RPC transactions.</param>
        /// <param name="userAgent">Information such as the application name, version, host operating system, and language.</param>
        /// <param name="message">A <see cref="XmlRpcMessage"/> that represents the information needed to execute the remote procedure call.</param>
        /// <param name="useDefaultCredentials">
        ///     Controls whether the <see cref="CredentialCache.DefaultCredentials">DefaultCredentials</see> are sent when making remote procedure calls.
        /// </param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="host"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="userAgent"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="userAgent"/> is an empty string.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="message"/> is a null reference (Nothing in Visual Basic).</exception>
        private static WebRequest CreateWebRequest(Uri host, string userAgent, XmlRpcMessage message, bool useDefaultCredentials, WebRequestOptions options)
        {
            HttpWebRequest httpRequest = null;

            byte[] payloadData;

            Guard.ArgumentNotNull(host, "host");
            Guard.ArgumentNotNullOrEmptyString(userAgent, "userAgent");
            Guard.ArgumentNotNull(message, "message");

            using (MemoryStream stream = new MemoryStream())
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.ConformanceLevel   = ConformanceLevel.Document;
                settings.Encoding           = message.Encoding;
                settings.Indent             = true;
                settings.OmitXmlDeclaration = false;

                using (XmlWriter writer = XmlWriter.Create(stream, settings))
                {
                    message.WriteTo(writer);
                    writer.Flush();
                }

                stream.Seek(0, SeekOrigin.Begin);
                payloadData = message.Encoding.GetBytes((new StreamReader(stream)).ReadToEnd());
            }

            httpRequest               = (HttpWebRequest)HttpWebRequest.Create(host);
            httpRequest.Method        = "POST";
            httpRequest.ContentLength = payloadData.Length;
            httpRequest.ContentType   = String.Format(null, "text/xml; charset={0}", message.Encoding.WebName);
            httpRequest.UserAgent     = userAgent;
            if (options != null)
            {
                options.ApplyOptions(httpRequest);
            }

            if (useDefaultCredentials)
            {
                httpRequest.Credentials = CredentialCache.DefaultCredentials;
            }

            using (Stream stream = httpRequest.GetRequestStream())
            {
                stream.Write(payloadData, 0, payloadData.Length);
            }

            return(httpRequest);
        }
Exemple #23
0
        internal static async Task <HttpResponseInfo> SendAsync(this LazyUri url, WebRequestOptions options, HttpRequestMessageBox messageBox, bool alwaysCatchAndForbidRedirects = false, bool keepResponseAliveOnError = false, bool synchronous = false)
        {
            HttpUtils.EnsureInitialized();
            if (!synchronous)
            {
                await Utils.CheckLocalFileAccessAsync(url);
            }
            Utils.RaiseWebRequestEvent(url, false);
            HttpResponseMessage result            = null;
            LazyUri             previousResponse2 = null;

            try
            {
                if (options == WebRequestOptions.DefaultOptions)
                {
                    throw new ArgumentException();
                }
                if (options.WaitBefore.Ticks != 0 && !synchronous)
                {
                    await TaskEx.Delay(options.WaitBefore);
                }
                LazyUri previousResponse1 = null;
                previousResponse2 = url.Clone();
                previousResponse2 = MaybeAddAdditionalQueryParameters(previousResponse2, options);
                var redirectIndex = 0;
                while (true)
                {
#if WEBCLIENT
                    HttpContent requestContent = null;
#endif
                    var message = messageBox?.PrebuiltRequest ?? CreateRequestInternal(previousResponse2, options, true, redirectIndex
#if WEBCLIENT
                                                                                       , out requestContent
#endif
                                                                                       );
                    if (messageBox != null)
                    {
                        messageBox.Dispose();
                        messageBox.Message = message;
                    }

#if WEBCLIENT
                    if (requestContent != null)
                    {
                        if (requestContent.ContentType != null)
                        {
                            message.ContentType = requestContent.ContentType;
                        }
                        if (requestContent.ContentDisposition != null)
                        {
                            message.Headers["Content-Disposition"] = requestContent.ContentDisposition;
                        }
                        using (var req = await message.GetRequestStreamAsync())
                        {
                            await requestContent.CopyToAsync(req);
                        }
                    }
                    result = (HttpWebResponse)await message.GetResponseAsync();
#else
                    message.Properties["ShamanURL"] = url;
                    if (options.CustomHttpClient != null)
                    {
                        result = await options.CustomHttpClient.SendAsync(message, HttpCompletionOption.ResponseHeadersRead);
                    }
                    else
                    {
                        if (defaultHttpClient == null)
                        {
                            defaultHttpClient = CreateHttpClient();
                        }

                        result = messageBox?.PrebuiltResponse ?? await defaultHttpClient.SendAsync(message, HttpCompletionOption.ResponseHeadersRead);
                    }
#endif



#if !WEBCLIENT
                    if (result.Content != null && result.Content.Headers.ContentType != null && result.Content.Headers.ContentType.CharSet == "utf8")
                    {
                        result.Content.Headers.ContentType.CharSet = "utf-8";
                    }
#endif

                    if ((int)result.StatusCode >= 400)
                    {
                        if (!keepResponseAliveOnError)
                        {
                            result.Dispose();
                        }
                        // Hackish, change purpose of enumeration type
                        throw new WebException("The web server returned: " + result.StatusCode.ToString(), (WebExceptionStatus)result.StatusCode);
                    }
#if WEBCLIENT
                    var zz = result.Headers["Location"];
                    var redirectUrlNative = zz != null?HttpUtils.GetAbsoluteUri(url.PathConsistentUrl, zz) : null;
#else
                    var redirectUrlNative = result.Headers.Location;
#endif

                    if (redirectUrlNative == null)
                    {
                        return(new HttpResponseInfo()
                        {
                            RespondingUrl = previousResponse2, Response = result
                        });
                    }
                    else
                    {
                        if (alwaysCatchAndForbidRedirects)
                        {
                            return new HttpResponseInfo()
                                   {
                                       Response = result, RespondingUrl = previousResponse2, Exception = new WebException("Unexpected redirect", HttpUtils.Error_UnexpectedRedirect)
                                   }
                        }
                        ;

                        result.Dispose();
                        var redirectUrl = new LazyUri(redirectUrlNative);
                        if (!redirectUrl.IsAbsoluteUri)
                        {
                            redirectUrl = new LazyUri(new Uri(previousResponse2.PathConsistentUrl, redirectUrlNative));
                        }
                        if (options != null && !options.AllowRedirects)
                        {
                            throw new WebException("Unexpected redirect was received.", HttpUtils.Error_UnexpectedRedirect);
                        }
                        if (redirectIndex == Configuration_MaximumNumberOfRedirects)
                        {
                            throw new WebException("The maximum number of redirects has been reached.", HttpUtils.Error_MaximumNumberOfRedirectsExceeded);
                        }

                        if (!(redirectIndex == 0 && options != null && (options.PostData != null || options.PostString != null)))
                        {
                            if ((
                                    (previousResponse1 != null && HttpUtils.UrisEqual(redirectUrl.PathAndQueryConsistentUrl, previousResponse1.PathAndQueryConsistentUrl)) ||
                                    HttpUtils.UrisEqual(redirectUrl, previousResponse2)))
                            {
                                if (url.GetFragmentParameter("$allow-same-redirect") == "1")
                                {
                                    if (!synchronous)
                                    {
#if NET35
                                        await TaskEx.Delay(Configuration_SameRedirectDelayTimeMs);
#else
                                        await Task.Delay(Configuration_SameRedirectDelayTimeMs);
#endif
                                    }
                                }
                                else
                                {
                                    throw new WebException("The server isn't redirecting the requested resource properly.", HttpUtils.Error_RedirectLoopDetected);
                                }
                            }
                        }
                        previousResponse1 = previousResponse2;
                        previousResponse2 = redirectUrl;

                        redirectIndex++;
                    }
                }
            }
            catch (Exception ex)
            {
                var orig = ex;
#if !WEBCLIENT
                var hre = ex as HttpRequestException;
                if (hre != null && hre.InnerException != null)
                {
                    ex = hre.InnerException;
                }
#endif
                if (alwaysCatchAndForbidRedirects)
                {
                    return new HttpResponseInfo()
                           {
                               Exception = ex, Response = result, RespondingUrl = previousResponse2
                           }
                }
                ;
                else if (ex == orig)
                {
                    throw;
                }
                else
                {
                    throw ex.Rethrow();
                }
            }
        }
        /// <summary>
        /// Called when a corresponding asynchronous load operation completes.
        /// </summary>
        /// <param name="result">The result of the asynchronous operation.</param>
        private static void AsyncLoadCallback(IAsyncResult result)
        {
            System.Text.Encoding encoding       = System.Text.Encoding.UTF8;
            XPathNavigator       navigator      = null;
            WebRequest           httpWebRequest = null;
            AtomEntryResource    entry          = null;
            Uri source = null;
            WebRequestOptions options = null;
            SyndicationResourceLoadSettings settings = null;

            if (result.IsCompleted)
            {
                object[] parameters = (object[])result.AsyncState;
                httpWebRequest = parameters[0] as WebRequest;
                entry          = parameters[1] as AtomEntryResource;
                source         = parameters[2] as Uri;
                settings       = parameters[3] as SyndicationResourceLoadSettings;
                options        = parameters[4] as WebRequestOptions;
                object userToken = parameters[5];

                if (entry != null)
                {
                    WebResponse httpWebResponse = (WebResponse)httpWebRequest.EndGetResponse(result);

                    using (Stream stream = httpWebResponse.GetResponseStream())
                    {
                        if (settings != null)
                        {
                            encoding = settings.CharacterEncoding;
                        }

                        using (StreamReader streamReader = new StreamReader(stream, encoding))
                        {
                            XmlReaderSettings readerSettings = new XmlReaderSettings();
                            readerSettings.IgnoreComments   = true;
                            readerSettings.IgnoreWhitespace = true;
                            readerSettings.DtdProcessing    = DtdProcessing.Ignore;

                            using (XmlReader reader = XmlReader.Create(streamReader, readerSettings))
                            {
                                if (encoding == System.Text.Encoding.UTF8)
                                {
                                    navigator = SyndicationEncodingUtility.CreateSafeNavigator(source, options, null);
                                }
                                else
                                {
                                    navigator = SyndicationEncodingUtility.CreateSafeNavigator(source, options, settings.CharacterEncoding);
                                }

                                SyndicationResourceAdapter adapter = new SyndicationResourceAdapter(navigator, settings);
                                adapter.Fill(entry, SyndicationContentFormat.Atom);

                                AtomPublishingEditedSyndicationExtension editedExtension = entry.FindExtension(AtomPublishingEditedSyndicationExtension.MatchByType) as AtomPublishingEditedSyndicationExtension;
                                if (editedExtension != null)
                                {
                                    entry.EditedOn = editedExtension.Context.EditedOn;
                                }

                                AtomPublishingControlSyndicationExtension controlExtension = entry.FindExtension(AtomPublishingControlSyndicationExtension.MatchByType) as AtomPublishingControlSyndicationExtension;
                                if (controlExtension != null)
                                {
                                    entry.IsDraft = controlExtension.Context.IsDraft;
                                }

                                entry.OnEntryLoaded(new SyndicationResourceLoadedEventArgs(navigator, source, options, userToken));
                            }
                        }
                    }

                    entry.LoadOperationInProgress = false;
                }
            }
        }
        public object MakeWebRequestEx(object apiUrl, object apiMethod, dynamic options, dynamic values)
        {
            if (!(apiUrl is string apiUrlText))
            {
                throw new ApplicationException($"{nameof(MakeWebRequestEx)}: {nameof(apiUrl)} must be a string");
            }
            if (!(apiMethod is string apiMethodText))
            {
                throw new ApplicationException($"{nameof(MakeWebRequestEx)}: {nameof(apiMethod)} must be a string");
            }

            int tries       = 0;
            int dnsAttempts = 1;
            WebRequestOptions webRequestOptions = ProcessWebRequestOptions(options);

            do
            {
                logger.InfoFormat("Script: Calling web url: {0}", apiUrlText);
                Uri uri = new Uri(apiUrlText);  // Makes sure the url is valid

                try
                {
                    // Using older WebClient class since it has non-async methods.
                    // We can't use Async calls here because we aren't passing the async/await context through javscript.
                    // That requires using the javascript callback method and makes the javascript more complex.
                    using (WebClientEx wc = new WebClientEx(webRequestOptions.Timeout))
                    {
                        // fix for possible performance issue with WebClient class
                        // https://stackoverflow.com/questions/4932541/c-sharp-webclient-acting-slow-the-first-time
                        wc.Proxy = null;

                        Stopwatch sw = new Stopwatch();
                        sw.Start();

                        if (DummyMode)
                        {
                            wc.Headers.Add("X-DummyMode", "True");
                        }

                        // Add headers
                        if (webRequestOptions.Headers != null)
                        {
                            foreach (var header in webRequestOptions.Headers)
                            {
                                wc.Headers.Add(header.Key, header.Value?.ToString());
                            }
                        }

                        string response;

                        if (apiMethodText.Equals("GET", StringComparison.OrdinalIgnoreCase))
                        {
                            response = wc.DownloadString(uri);
                        }
                        else
                        {
                            string data = null;
                            if (webRequestOptions.KeepRequestAsString)
                            {
                                data = values;
                            }
                            else
                            {
                                var native = ConvertToNative(values);
                                if (native != null)
                                {
                                    data = JsonConvert.SerializeObject(native);
                                }
                            }

                            logger.DebugFormat("Script: Request: {0}", data);
                            response = wc.UploadString(uri, apiMethodText, data);
                        }

                        logger.DebugFormat("Script: Response: {0}", response);

                        sw.Stop();
                        var elapsedTime = sw.Elapsed.TotalSeconds;
                        if ((ChatConfiguration.DefaultRestAPIWarnSendTime >= 0) && (elapsedTime > ChatConfiguration.DefaultRestAPIWarnSendTime))
                        {
                            logger.WarnFormat("Network: MakeWebRequestEx() took {0} seconds. Url: {1}", elapsedTime, uri);
                        }

                        if (webRequestOptions.KeepResponseAsString)
                        {
                            return(response);
                        }

                        var result = JsonConvert.DeserializeObject <dynamic>(response);
                        return(ScriptHelpers.ToScriptValue(result, ScriptContext));
                    }
                }
                catch (WebException ex)
                {
                    if (ex.Status == WebExceptionStatus.NameResolutionFailure)
                    {
                        // WebClient gets DNS failures sometimes, so we auto retry.
                        if (++dnsAttempts <= MaxDnsAttempts)
                        {
                            logger.WarnFormat("Script: Warning DNS failure. Retrying: {0}", apiUrl);

                            // dont count as regulare try
                            tries--;
                            continue;
                        }

                        logger.ErrorFormat("Script: Error calling url {0}. {1}, {2}", apiUrl, ex.Status, ex.ToString());
                        return(null);
                    }

                    logger.ErrorFormat("Script: Error calling url {0}. {1}, {2}", apiUrl, ex.Status, ex.ToString());

                    if (ex.Status == WebExceptionStatus.ProtocolError)
                    {
                        var webResponse = (HttpWebResponse)ex.Response;

                        var errorBody = new StreamReader(webResponse.GetResponseStream()).ReadToEnd();
                        logger.DebugFormat("Script: Error Response: {0}", errorBody);

                        if (((int)webResponse.StatusCode >= 400) && ((int)webResponse.StatusCode <= 499))
                        {
                            // log 400 error. not re-try-able);
                            break;
                        }
                    }
                }
            } while (tries++ < webRequestOptions.Retries);

            return(null);
        }
Exemple #26
0
        private static HttpRequestMessage CreateRequestInternal(LazyUri url, WebRequestOptions options, bool forceSameUrl, int redirectionIndex
#if WEBCLIENT
        , out HttpContent requestContent
#endif
        )
        {
            if (!forceSameUrl) url = MaybeAddAdditionalQueryParameters(url, options);
#if WEBCLIENT
            var message = (HttpWebRequest)WebRequest.Create(url.PathAndQueryConsistentUrl);
            message.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
            message.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.Deflate;
            requestContent = null;
#else
            var message = new HttpRequestMessage();
            message.RequestUri = url.PathAndQueryConsistentUrl;
#endif
            if (options != null)
            {
                if (redirectionIndex != 0) message.Method = HttpMethod.Get;
                else if (options._method != null)
#if WEBCLIENT 
                    message.Method = options._method;
#else
                    message.Method = new HttpMethod(options._method);
#endif
                else message.Method = (options.PostData != null || options.PostString != null || options.WriteRequest != null) ? HttpMethod.Post : HttpMethod.Get;
            }
Exemple #27
0
        internal static async Task<HttpResponseInfo> SendAsync(this LazyUri url, WebRequestOptions options, HttpRequestMessageBox messageBox, bool alwaysCatchAndForbidRedirects = false, bool keepResponseAliveOnError = false)
        {
            await Utils.CheckLocalFileAccessAsync(url);
            Utils.RaiseWebRequestEvent(url, true);
            HttpResponseMessage result = null;
            LazyUri previousResponse2 = null;
            try
            {

                if (options == WebRequestOptions.DefaultOptions) throw new ArgumentException();
                if (options.WaitBefore.Ticks != 0)
                    await TaskEx.Delay(options.WaitBefore);
                LazyUri previousResponse1 = null;
                previousResponse2 = url.Clone();
                previousResponse2 = MaybeAddAdditionalQueryParameters(previousResponse2, options);
                var redirectIndex = 0;
                while (true)
                {
#if WEBCLIENT
                    HttpContent requestContent;
#endif
                    var message = CreateRequestInternal(previousResponse2, options, true, redirectIndex
#if WEBCLIENT
                    , out requestContent
#endif              
                    );
                    if (messageBox != null)
                    {
                        messageBox.Dispose();
                        messageBox.Message = message;
                    }

#if WEBCLIENT
                    if(requestContent != null)
                    {
                        if(requestContent.ContentType != null) message.ContentType = requestContent.ContentType;
                        if(requestContent.ContentDisposition != null) message.Headers["Content-Disposition"] = requestContent.ContentDisposition;
                        using(var req = await message.GetRequestStreamAsync())
                        {
                            await requestContent.CopyToAsync(req);
                        }
                    }
                    result = (HttpWebResponse)await message.GetResponseAsync();
#else
                    var client = CreateHttpClient();
                    result = await client.SendAsync(message, HttpCompletionOption.ResponseHeadersRead);
#endif





#if !WEBCLIENT
                    if (result.Content != null && result.Content.Headers.ContentType != null && result.Content.Headers.ContentType.CharSet == "utf8")
                        result.Content.Headers.ContentType.CharSet = "utf-8";
#endif

                    if ((int)result.StatusCode >= 400)
                    {
                        if(!keepResponseAliveOnError) result.Dispose();
                        // Hackish, change purpose of enumeration type
                        throw new WebException("The web server returned: " + result.StatusCode.ToString(), (WebExceptionStatus)result.StatusCode);
                    }
#if WEBCLIENT
                    var zz = result.Headers["Location"];
                    var redirectUrlNative = zz != null ? HttpUtils.GetAbsoluteUri(url.PathConsistentUrl, zz) : null;
#else
                    var redirectUrlNative = result.Headers.Location;
#endif

                    if (redirectUrlNative == null)
                    {
                        return new HttpResponseInfo() { RespondingUrl = previousResponse2, Response = result };

                    }
                    else
                    {

                        if (alwaysCatchAndForbidRedirects) return new HttpResponseInfo() { Response = result, RespondingUrl = previousResponse2, Exception = new WebException("Unexpected redirect", HttpUtils.UnexpectedRedirect) };

                        result.Dispose();
                        var redirectUrl = new LazyUri(redirectUrlNative);
                        if (!redirectUrl.IsAbsoluteUri) redirectUrl = new LazyUri(new Uri(previousResponse2.PathConsistentUrl, redirectUrlNative));
                        if (options != null && !options.AllowRedirects) throw new WebException("Unexpected redirect was received.", HttpUtils.UnexpectedRedirect);
                        if (redirectIndex == Configuration_MaximumNumberOfRedirects) throw new WebException("The maximum number of redirects has been reached.", HttpUtils.MaximumNumberOfRedirectsExceeded);

                        if (!(redirectIndex == 0 && options != null && (options.PostData != null || options.PostString != null)))
                            if ((previousResponse1 != null && HttpUtils.UrisEqual(redirectUrl.PathAndQueryConsistentUrl, previousResponse1.PathAndQueryConsistentUrl)) || HttpUtils.UrisEqual(redirectUrl, previousResponse2)) throw new WebException("The server isn't redirecting the requested resource properly.", HttpUtils.RedirectLoopDetected);
                        previousResponse1 = previousResponse2;
                        previousResponse2 = redirectUrl;

                        redirectIndex++;


                    }
                }
            }
            catch (Exception ex)
            {
                var orig = ex;
#if !WEBCLIENT
                var hre = ex as HttpRequestException;
                if (hre != null && hre.InnerException != null) ex = hre.InnerException;
#endif
                if (alwaysCatchAndForbidRedirects) return new HttpResponseInfo() { Exception = ex, Response = result, RespondingUrl = previousResponse2 };
                else if (ex == orig) throw;
                else throw ex;
            }
        }
Exemple #28
0
 public async Task<HttpResponseMessage> GetResponseAsync(long startPosition)
 {
     if (partialDownload != null && startPosition == 0)
     {
         var c = partialDownload;
         partialDownload = null;
         return c;
     }
     var options = new WebRequestOptions()
     {
         Timeout = 30000,
         TimeoutSecondRetrialAfterError = 10000,
         TimeoutStartSecondRetrial = null
     };
     var url = new LazyUri(this.Url);
     HttpExtensionMethods.ProcessMetaParameters(url, options);
     if (startPosition != 0) options.AddHeader("Range", "bytes=" + startPosition + "-");
     return await HttpExtensionMethods.GetResponseAsync(url, options);
     //return (await HttpExtensionMethods.SendAsync(url, options, null)).Response;
 }
        /// <summary>
        /// Loads this <see cref="AtomEntry"/> instance asynchronously using the specified <see cref="Uri"/>, <see cref="SyndicationResourceLoadSettings"/>, <see cref="ICredentials"/>, and <see cref="IWebProxy"/>.
        /// </summary>
        /// <param name="source">A <see cref="Uri"/> that represents the URL of the syndication resource XML data.</param>
        /// <param name="settings">The <see cref="SyndicationResourceLoadSettings"/> object used to configure the <see cref="AtomEntry"/> instance. This value can be <b>null</b>.</param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
        /// <param name="userToken">A user-defined object that is passed to the method invoked when the asynchronous operation completes.</param>
        /// <remarks>
        ///     <para>
        ///         To receive notification when the operation has completed or the operation has been canceled, add an event handler to the <see cref="AtomEntry.Loaded"/> event.
        ///         You can cancel a <see cref="LoadAsync(Uri, SyndicationResourceLoadSettings, ICredentials, IWebProxy, Object)"/> operation by calling the <see cref="LoadAsyncCancel()"/> method.
        ///     </para>
        ///     <para>
        ///         After calling <see cref="LoadAsync(Uri, SyndicationResourceLoadSettings, ICredentials, IWebProxy, Object)"/>,
        ///         you must wait for the load operation to complete before attempting to load the syndication resource using the <see cref="AtomEntry.LoadAsync(Uri, Object)"/> method.
        ///     </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="FormatException">The <paramref name="source"/> data does not conform to the expected syndication content format. In this case, the feed remains empty.</exception>
        /// <exception cref="InvalidOperationException">This <see cref="AtomEntry"/> has a <see cref="LoadAsync(Uri, SyndicationResourceLoadSettings, ICredentials, IWebProxy, Object)"/> call in progress.</exception>
        public new void LoadAsync(Uri source, SyndicationResourceLoadSettings settings, WebRequestOptions options, Object userToken)
        {
            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(source, "source");

            //------------------------------------------------------------
            //	Use default settings if none specified by the caller
            //------------------------------------------------------------
            if (settings == null)
            {
                settings = new SyndicationResourceLoadSettings();
            }

            //------------------------------------------------------------
            //	Validate syndication resource state
            //------------------------------------------------------------
            if (this.LoadOperationInProgress)
            {
                throw new InvalidOperationException();
            }

            //------------------------------------------------------------
            //	Indicate that a load operation is in progress
            //------------------------------------------------------------
            this.LoadOperationInProgress = true;

            //------------------------------------------------------------
            //	Reset the asynchronous load operation cancelled indicator
            //------------------------------------------------------------
            this.AsyncLoadHasBeenCancelled = false;

            //------------------------------------------------------------
            //	Build HTTP web request used to retrieve the syndication resource
            //------------------------------------------------------------
            asyncHttpWebRequest         = SyndicationEncodingUtility.CreateWebRequest(source, options);
            asyncHttpWebRequest.Timeout = Convert.ToInt32(settings.Timeout.TotalMilliseconds, System.Globalization.NumberFormatInfo.InvariantInfo);

            //------------------------------------------------------------
            //	Get the async response to the web request
            //------------------------------------------------------------
            object[] state = new object[6] {
                asyncHttpWebRequest, this, source, settings, options, userToken
            };
            IAsyncResult result = asyncHttpWebRequest.BeginGetResponse(new AsyncCallback(AsyncLoadCallback), state);

            //------------------------------------------------------------
            //  Register the timeout callback
            //------------------------------------------------------------
            ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(AsyncTimeoutCallback), state, settings.Timeout, true);
        }
        //============================================================
        //	PRIVATE METHODS
        //============================================================
        #region CreateWebRequest(Uri host, string userAgent, TrackbackMessage message, bool useDefaultCredentials, WebRequestOptions options)
        /// <summary>
        /// Initializes a new <see cref="WebRequest"/> suitable for sending an Trackback ping request using the supplied host, user agent, message, credentials, and proxy.
        /// </summary>
        /// <param name="host">A <see cref="Uri"/> that represents the URL of the host computer used for Trackback transactions.</param>
        /// <param name="userAgent">Information such as the application name, version, host operating system, and language.</param>
        /// <param name="message">A <see cref="TrackbackMessage"/> that represents the information needed to execute the Trackback ping request.</param>
        /// <param name="useDefaultCredentials">
        ///     Controls whether the <see cref="CredentialCache.DefaultCredentials">DefaultCredentials</see> are sent when making Trackback pings.
        /// </param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="host"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="userAgent"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="userAgent"/> is an empty string.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="message"/> is a null reference (Nothing in Visual Basic).</exception>
        private static WebRequest CreateWebRequest(Uri host, string userAgent, TrackbackMessage message, bool useDefaultCredentials, WebRequestOptions options)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            HttpWebRequest httpRequest = null;

            byte[] payloadData;

            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(host, "host");
            Guard.ArgumentNotNullOrEmptyString(userAgent, "userAgent");
            Guard.ArgumentNotNull(message, "message");

            //------------------------------------------------------------
            //	Build the Trackback payload data
            //------------------------------------------------------------
            using (MemoryStream stream = new MemoryStream())
            {
                using (StreamWriter writer = new StreamWriter(stream, message.Encoding))
                {
                    message.WriteTo(writer);
                    writer.Flush();

                    stream.Seek(0, SeekOrigin.Begin);
                    payloadData = message.Encoding.GetBytes((new StreamReader(stream)).ReadToEnd());
                }
            }

            //------------------------------------------------------------
            //	Build Trackback web request used to send an Trackback ping request
            //------------------------------------------------------------
            httpRequest               = (HttpWebRequest)HttpWebRequest.Create(host);
            httpRequest.Method        = "POST";
            httpRequest.ContentLength = payloadData.Length;
            httpRequest.ContentType   = String.Format(null, "application/x-www-form-urlencoded; charset={0}", message.Encoding.WebName);
            httpRequest.UserAgent     = userAgent;
            if (options != null)
            {
                options.ApplyOptions(httpRequest);
            }

            if (useDefaultCredentials)
            {
                httpRequest.Credentials = CredentialCache.DefaultCredentials;
            }

            using (Stream stream = httpRequest.GetRequestStream())
            {
                stream.Write(payloadData, 0, payloadData.Length);
            }

            return(httpRequest);
        }
        //============================================================
        //	CALLBACK DELEGATE METHODS
        //============================================================
        #region AsyncLoadCallback(IAsyncResult result)
        /// <summary>
        /// Called when a corresponding asynchronous load operation completes.
        /// </summary>
        /// <param name="result">The result of the asynchronous operation.</param>
        private static void AsyncLoadCallback(IAsyncResult result)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            System.Text.Encoding encoding       = System.Text.Encoding.UTF8;
            XPathNavigator       navigator      = null;
            WebRequest           httpWebRequest = null;
            AtomEntryResource    entry          = null;
            Uri source = null;
            WebRequestOptions options = null;
            SyndicationResourceLoadSettings settings = null;

            //------------------------------------------------------------
            //	Determine if the async send operation completed
            //------------------------------------------------------------
            if (result.IsCompleted)
            {
                //------------------------------------------------------------
                //	Extract the send operations parameters from the user state
                //------------------------------------------------------------
                object[] parameters = (object[])result.AsyncState;
                httpWebRequest = parameters[0] as WebRequest;
                entry          = parameters[1] as AtomEntryResource;
                source         = parameters[2] as Uri;
                settings       = parameters[3] as SyndicationResourceLoadSettings;
                options        = parameters[4] as WebRequestOptions;
                object userToken = parameters[5];

                //------------------------------------------------------------
                //	Verify expected parameters were found
                //------------------------------------------------------------
                if (entry != null)
                {
                    //------------------------------------------------------------
                    //	Get the response to the syndication resource request
                    //------------------------------------------------------------
                    WebResponse httpWebResponse = (WebResponse)httpWebRequest.EndGetResponse(result);

                    //------------------------------------------------------------
                    //	Load syndication resource
                    //------------------------------------------------------------
                    using (Stream stream = httpWebResponse.GetResponseStream())
                    {
                        if (settings != null)
                        {
                            encoding = settings.CharacterEncoding;
                        }

                        using (StreamReader streamReader = new StreamReader(stream, encoding))
                        {
                            XmlReaderSettings readerSettings = new XmlReaderSettings();
                            readerSettings.IgnoreComments   = true;
                            readerSettings.IgnoreWhitespace = true;
                            readerSettings.ProhibitDtd      = false;

                            using (XmlReader reader = XmlReader.Create(streamReader, readerSettings))
                            {
                                if (encoding == System.Text.Encoding.UTF8)
                                {
                                    navigator = SyndicationEncodingUtility.CreateSafeNavigator(source, options, null);
                                }
                                else
                                {
                                    navigator = SyndicationEncodingUtility.CreateSafeNavigator(source, options, settings.CharacterEncoding);
                                }

                                //------------------------------------------------------------
                                //	Load syndication resource using the framework adapters
                                //------------------------------------------------------------
                                SyndicationResourceAdapter adapter = new SyndicationResourceAdapter(navigator, settings);
                                adapter.Fill(entry, SyndicationContentFormat.Atom);

                                //------------------------------------------------------------
                                //	Extract member resource information from extensions
                                //------------------------------------------------------------
                                AtomPublishingEditedSyndicationExtension editedExtension = entry.FindExtension(AtomPublishingEditedSyndicationExtension.MatchByType) as AtomPublishingEditedSyndicationExtension;
                                if (editedExtension != null)
                                {
                                    entry.EditedOn = editedExtension.Context.EditedOn;
                                }

                                AtomPublishingControlSyndicationExtension controlExtension = entry.FindExtension(AtomPublishingControlSyndicationExtension.MatchByType) as AtomPublishingControlSyndicationExtension;
                                if (controlExtension != null)
                                {
                                    entry.IsDraft = controlExtension.Context.IsDraft;
                                }

                                //------------------------------------------------------------
                                //	Raise Loaded event to notify registered handlers of state change
                                //------------------------------------------------------------
                                entry.OnEntryLoaded(new SyndicationResourceLoadedEventArgs(navigator, source, options, userToken));
                            }
                        }
                    }

                    //------------------------------------------------------------
                    //	Reset load operation in progress indicator
                    //------------------------------------------------------------
                    entry.LoadOperationInProgress = false;
                }
            }
        }
Exemple #32
0
 public static T GetJson <T>(this Uri url, WebRequestOptions options = null)
 {
     return(DeserializeJson <T>(url.GetString(options)));
 }
Exemple #33
0
 /// <summary>
 /// Loads the syndication resource from the supplied <see cref="Uri"/> using the specified <see cref="ICredentials">credentials</see> and <see cref="IWebProxy">proxy</see>.
 /// </summary>
 /// <param name="source">A <see cref="Uri"/> that points to the location of the web resource used to load the syndication resource.</param>
 /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
 /// <remarks>
 ///     <para>
 ///         <list type="bullet">
 ///             <item>
 ///                 <description>
 ///                     After the load operation has successfully completed, the <see cref="MyCustomRssFeed.Loaded"/> event will be raised.
 ///                 </description>
 ///             </item>
 ///         </list>
 ///     </para>
 /// </remarks>
 /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
 /// <exception cref="FormatException">The <paramref name="source"/> data does not conform to the expected syndication content format. In this case, the feed remains empty.</exception>
 /// <exception cref="XmlException">There is a load or parse error in the XML. In this case, the feed remains empty.</exception>
 public void Load(Uri source, WebRequestOptions options)
 {
     this.Load(source, options, null);
 }