/// <summary>
        /// Returns a <see cref="WebRequest"/> that makes a request for a resource located at 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 resource to be retrieved.</param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
        /// <returns>
        ///     An <see cref="WebRequest"/> that makes a request to the <paramref name="source"/>. If unable to create a <see cref="WebRequest"/> for
        ///     the specified <paramref name="source"/>, returns a <b>null</b> reference (Nothing in Visual Basic).
        /// </returns>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        public static WebRequest CreateWebRequest(Uri source, WebRequestOptions options)
        {
            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            WebRequest request = null;

            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(source, "source");

            request = WebRequest.Create(source);

            if (source.IsAbsoluteUri)
            {
                if (String.Compare(source.Scheme, Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase) == 0 ||
                    String.Compare(source.Scheme, Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    HttpWebRequest httpRequest = (HttpWebRequest)request;
                    httpRequest.UserAgent = SyndicationDiscoveryUtility.FrameworkUserAgent;
                    request = httpRequest;
                }
            }

            if (options != null)
            {
                options.ApplyOptions(request);
            }
            return(request);
        }
 /// <summary>
 /// Creates a <see cref="XPathNavigator"/> against 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 XML data to be navigated by the created <see cref="XPathNavigator"/>.</param>
 /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
 /// <returns>
 ///     An <see cref="XPathNavigator"/> that provides a cursor model for navigating the supplied <paramref name="source"/>.
 ///     The supplied <paramref name="source"/> XML data is parsed to remove invalid XML characters that would normally prevent
 ///     a navigator from being created.
 /// </returns>
 /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
 public static XPathNavigator CreateSafeNavigator(Uri source, WebRequestOptions options)
 {
     //------------------------------------------------------------
     //	Create safe navigator using auto-detection of encoding
     //------------------------------------------------------------
     return(SyndicationEncodingUtility.CreateSafeNavigator(source, options, null));
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="SyndicationResourceLoadedEventArgs"/> class using the supplied <see cref="IXPathNavigable"/>, <see cref="ICredentials">credentials</see> and <see cref="IWebProxy">proxy</see>.
        /// </summary>
        /// <param name="data">A <see cref="IXPathNavigable"/> object that represents the XML data that was used to load the syndication resource.</param>
        /// <param name="source">
        ///     The <see cref="Uri"/> of the Internet resource that the syndication resource was loaded from. Can be <b>null</b> if syndication resource was not loaded using an Internet resource.
        /// </param>
        /// <param name="credentials">
        ///    The <see cref="ICredentials"/> that were used to authenticate the request to an Internet resource. Can be <b>null</b> if syndication resource was not loaded using an Internet resource.
        /// </param>
        /// <param name="proxy">
        ///     The <see cref="IWebProxy"/> used to access the Internet resource. Can be <b>null</b> if syndication resource was not loaded using an Internet resource.
        /// </param>
        /// <exception cref="ArgumentNullException">The <paramref name="data"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        public SyndicationResourceLoadedEventArgs(IXPathNavigable data, Uri source, ICredentials credentials, IWebProxy proxy) : this(data)
        {
            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(source, "source");

            eventSource  = source;
            eventOptions = new WebRequestOptions(credentials, proxy);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SyndicationResourceLoadedEventArgs"/> class using the supplied <see cref="IXPathNavigable"/>, <see cref="ICredentials">credentials</see> and <see cref="IWebProxy">proxy</see>.
        /// </summary>
        /// <param name="data">A <see cref="IXPathNavigable"/> object that represents the XML data that was used to load the syndication resource.</param>
        /// <param name="source">
        ///     The <see cref="Uri"/> of the Internet resource that the syndication resource was loaded from. Can be <b>null</b> if syndication resource was not loaded using an Internet resource.
        /// </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="data"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        public SyndicationResourceLoadedEventArgs(IXPathNavigable data, Uri source, WebRequestOptions options) : this(data)
        {
            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(source, "source");

            eventSource  = source;
            eventOptions = options ?? new WebRequestOptions();
        }
        /// <summary>
        /// Creates a <see cref="XPathNavigator"/> against 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 XML data to be navigated by the created <see cref="XPathNavigator"/>.</param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
        /// <param name="encoding">A <see cref="Encoding"/> object that indicates the expected character encoding of the supplied <paramref name="source"/>. This value can be <b>null</b>.</param>
        /// <returns>
        ///     An <see cref="XPathNavigator"/> that provides a cursor model for navigating the supplied <paramref name="source"/>.
        ///     The supplied <paramref name="source"/> XML data is parsed to remove invalid XML characters that would normally prevent
        ///     a navigator from being created.
        /// </returns>
        /// <remarks>
        ///     If the <paramref name="encoding"/> is <b>null</b>, the character encoding of the supplied <paramref name="source"/> is determined automatically.
        ///     Otherwise the specified <paramref name="encoding"/> is used when reading the XML data represented by the supplied <paramref name="source"/>.
        /// </remarks>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        public static XPathNavigator CreateSafeNavigator(Uri source, WebRequestOptions options, Encoding encoding)
        {
            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(source, "source");

            using (WebResponse response = SyndicationEncodingUtility.CreateWebResponse(source, options))
            {
                Stream          stream       = null;
                HttpWebResponse httpResponse = response as HttpWebResponse;

                if (httpResponse != null)
                {
                    string contentEncoding = httpResponse.ContentEncoding.ToUpperInvariant();

                    if (contentEncoding.Contains("GZIP"))
                    {
                        stream = new GZipStream(httpResponse.GetResponseStream(), CompressionMode.Decompress);
                    }
                    else if (contentEncoding.Contains("DEFLATE"))
                    {
                        stream = new DeflateStream(httpResponse.GetResponseStream(), CompressionMode.Decompress);
                    }
                    else
                    {
                        stream = httpResponse.GetResponseStream();
                    }
                }
                else
                {
                    stream = response.GetResponseStream();
                }

                if (encoding != null)
                {
                    return(SyndicationEncodingUtility.CreateSafeNavigator(stream, encoding));
                }
                else
                {
                    return(SyndicationEncodingUtility.CreateSafeNavigator(stream));
                }
            }
        }
        /// <summary>
        /// Returns the <see cref="WebResponse"/> to a request for a resource located at 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 resource to be retrieved.</param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
        /// <returns>
        ///     An <see cref="WebResponse"/> that contains the response from the requested resource. If unable to create a <see cref="WebResponse"/> for
        ///     the requested <paramref name="source"/>, returns a <b>null</b> reference (Nothing in Visual Basic).
        /// </returns>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        public static WebResponse CreateWebResponse(Uri source, WebRequestOptions options)
        {
            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            WebResponse response = null;

            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(source, "source");

            WebRequest webRequest = SyndicationEncodingUtility.CreateWebRequest(source, options);

            if (webRequest != null)
            {
                response = webRequest.GetResponse();
            }

            return(response);
        }
        /// <summary>
        /// Returns a <see cref="WebRequest"/> that makes a request for a resource located at 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 resource to be retrieved.</param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
        /// <returns>
        ///     An <see cref="WebRequest"/> that makes a request to the <paramref name="source"/>. If unable to create a <see cref="WebRequest"/> for 
        ///     the specified <paramref name="source"/>, returns a <b>null</b> reference (Nothing in Visual Basic).
        /// </returns>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        public static WebRequest CreateWebRequest(Uri source, WebRequestOptions options)
        {
            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            WebRequest request  = null;

            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(source, "source");

            request             = WebRequest.Create(source);

            if(source.IsAbsoluteUri)
            {
                if (String.Compare(source.Scheme, Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase) == 0 ||
                    String.Compare(source.Scheme, Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    HttpWebRequest httpRequest      = (HttpWebRequest)request;
                    httpRequest.UserAgent           = SyndicationDiscoveryUtility.FrameworkUserAgent;
                    request                         = httpRequest;
                }
            }

            if (options != null) options.ApplyOptions(request);
            return request;
        }
Exemplo n.º 8
0
        /// <summary>
        /// Loads this <see cref="AtomFeed"/> 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="AtomFeed"/> 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="AtomFeed"/> 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)
        {
            //------------------------------------------------------------
            //	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);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SyndicationResourceLoadedEventArgs"/> class using the supplied <see cref="IXPathNavigable"/>, <see cref="ICredentials">credentials</see> and <see cref="IWebProxy">proxy</see>.
        /// </summary>
        /// <param name="data">A <see cref="IXPathNavigable"/> object that represents the XML data that was used to load the syndication resource.</param>
        /// <param name="source">
        ///     The <see cref="Uri"/> of the Internet resource that the syndication resource was loaded from. Can be <b>null</b> if syndication resource was not loaded using an Internet resource.
        /// </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="data"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        public SyndicationResourceLoadedEventArgs(IXPathNavigable data, Uri source, WebRequestOptions options)
            : this(data)
        {
            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(source, "source");

            eventSource         = source;
            eventOptions        = options ?? new WebRequestOptions();
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SyndicationResourceLoadedEventArgs"/> class using the supplied <see cref="IXPathNavigable"/>, <see cref="ICredentials">credentials</see>, <see cref="IWebProxy">proxy</see> and user token.
 /// </summary>
 /// <param name="data">A <see cref="IXPathNavigable"/> object that represents the XML data that was used to load the syndication resource.</param>
 /// <param name="source">
 ///     The <see cref="Uri"/> of the Internet resource that the syndication resource was loaded from. Can be <b>null</b> if syndication resource was not loaded using an Internet resource.
 /// </param>
 /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
 /// <param name="state">The user-defined object that was passed to the asynchronous operation.</param>
 /// <exception cref="ArgumentNullException">The <paramref name="data"/> is a null reference (Nothing in Visual Basic).</exception>
 /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
 public SyndicationResourceLoadedEventArgs(IXPathNavigable data, Uri source, WebRequestOptions options, Object state)
     : this(data, source, options)
 {
     eventUserToken  = state;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SyndicationResourceLoadedEventArgs"/> class using the supplied <see cref="IXPathNavigable"/>, <see cref="ICredentials">credentials</see>, <see cref="IWebProxy">proxy</see> and user token.
 /// </summary>
 /// <param name="data">A <see cref="IXPathNavigable"/> object that represents the XML data that was used to load the syndication resource.</param>
 /// <param name="source">
 ///     The <see cref="Uri"/> of the Internet resource that the syndication resource was loaded from. Can be <b>null</b> if syndication resource was not loaded using an Internet resource.
 /// </param>
 /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
 /// <param name="state">The user-defined object that was passed to the asynchronous operation.</param>
 /// <exception cref="ArgumentNullException">The <paramref name="data"/> is a null reference (Nothing in Visual Basic).</exception>
 /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
 public SyndicationResourceLoadedEventArgs(IXPathNavigable data, Uri source, WebRequestOptions options, Object state) : this(data, source, options)
 {
     eventUserToken = state;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="SyndicationResourceLoadedEventArgs"/> class using the supplied <see cref="IXPathNavigable"/>, <see cref="ICredentials">credentials</see> and <see cref="IWebProxy">proxy</see>.
        /// </summary>
        /// <param name="data">A <see cref="IXPathNavigable"/> object that represents the XML data that was used to load the syndication resource.</param>
        /// <param name="source">
        ///     The <see cref="Uri"/> of the Internet resource that the syndication resource was loaded from. Can be <b>null</b> if syndication resource was not loaded using an Internet resource.
        /// </param>
        /// <param name="credentials">
        ///    The <see cref="ICredentials"/> that were used to authenticate the request to an Internet resource. Can be <b>null</b> if syndication resource was not loaded using an Internet resource.
        /// </param>
        /// <param name="proxy">
        ///     The <see cref="IWebProxy"/> used to access the Internet resource. Can be <b>null</b> if syndication resource was not loaded using an Internet resource.
        /// </param>
        /// <exception cref="ArgumentNullException">The <paramref name="data"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        public SyndicationResourceLoadedEventArgs(IXPathNavigable data, Uri source, ICredentials credentials, IWebProxy proxy)
            : this(data)
        {
            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(source, "source");

            eventSource         = source;
            eventOptions        = new WebRequestOptions(credentials, proxy);
        }
        /// <summary>
        /// Performs a conditional get operation against the supplied <see cref="Uri"/> using the specified <see cref="DateTime"/>, entity tag and <see cref="ICredentials">credentials</see>.
        /// </summary>
        /// <param name="source">The <see cref="Uri"/> to perform a conditional GET operation against.</param>
        /// <param name="lastModified">A <see cref="DateTime"/> object that represents the date and time at which the <paramref name="source"/> was last known to be modified.</param>
        /// <param name="entityTag">The entity tag provided by the <paramref name="source"/> that is used to determine change in content.</param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
        /// <returns>A <see cref="HttpWebResponse"/> for the <paramref name="source"/> if it has been modfied, otherwise <b>null</b>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        public static HttpWebResponse ConditionalGet(Uri source, DateTime lastModified, string entityTag, WebRequestOptions options)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            HttpWebResponse response    = null;

            //------------------------------------------------------------
            //	Attempt to perform conditional get operation
            //------------------------------------------------------------
            if (SyndicationDiscoveryUtility.TryConditionalGet(source, lastModified, entityTag, options, out response))
            {
                return response;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// Returns the <see cref="WebResponse"/> to a request for a resource located at 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 resource to be retrieved.</param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
        /// <returns>
        ///     An <see cref="WebResponse"/> that contains the response from the requested resource. If unable to create a <see cref="WebResponse"/> for 
        ///     the requested <paramref name="source"/>, returns a <b>null</b> reference (Nothing in Visual Basic).
        /// </returns>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        public static WebResponse CreateWebResponse(Uri source, WebRequestOptions options)
        {
            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            WebResponse response    = null;

            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(source, "source");

            WebRequest webRequest   = SyndicationEncodingUtility.CreateWebRequest(source, options);
            if (webRequest != null)
            {
                response    = webRequest.GetResponse();
            }

            return response;
        }
        /// <summary>
        /// Creates a <see cref="XPathNavigator"/> against 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 XML data to be navigated by the created <see cref="XPathNavigator"/>.</param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
        /// <param name="encoding">A <see cref="Encoding"/> object that indicates the expected character encoding of the supplied <paramref name="source"/>. This value can be <b>null</b>.</param>
        /// <returns>
        ///     An <see cref="XPathNavigator"/> that provides a cursor model for navigating the supplied <paramref name="source"/>. 
        ///     The supplied <paramref name="source"/> XML data is parsed to remove invalid XML characters that would normally prevent 
        ///     a navigator from being created.
        /// </returns>
        /// <remarks>
        ///     If the <paramref name="encoding"/> is <b>null</b>, the character encoding of the supplied <paramref name="source"/> is determined automatically. 
        ///     Otherwise the specified <paramref name="encoding"/> is used when reading the XML data represented by the supplied <paramref name="source"/>.
        /// </remarks>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        public static XPathNavigator CreateSafeNavigator(Uri source, WebRequestOptions options, Encoding encoding)
        {
            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(source, "source");

            using (WebResponse response = SyndicationEncodingUtility.CreateWebResponse(source, options))
            {
                Stream stream                   = null;
                HttpWebResponse httpResponse    = response as HttpWebResponse;

                if (httpResponse != null)
                {
                    string contentEncoding  = httpResponse.ContentEncoding.ToUpperInvariant();

                    if (contentEncoding.Contains("GZIP"))
                    {
                        stream  = new GZipStream(httpResponse.GetResponseStream(), CompressionMode.Decompress);
                    }
                    else if (contentEncoding.Contains("DEFLATE"))
                    {
                        stream  = new DeflateStream(httpResponse.GetResponseStream(), CompressionMode.Decompress);
                    }
                    else
                    {
                        stream  = httpResponse.GetResponseStream();
                    }
                }
                else
                {
                    stream      = response.GetResponseStream();
                }

                if (encoding != null)
                {
                    return SyndicationEncodingUtility.CreateSafeNavigator(stream, encoding);
                }
                else
                {
                    return SyndicationEncodingUtility.CreateSafeNavigator(stream);
                }
            }
        }
        /// <summary>
        /// Performs a conditional get operation against the supplied <see cref="Uri"/> using the specified <see cref="DateTime"/>, entity tag and <see cref="ICredentials">credentials</see>.
        /// </summary>
        /// <param name="source">The <see cref="Uri"/> to perform a conditional GET operation against.</param>
        /// <param name="lastModified">A <see cref="DateTime"/> object that represents the date and time at which the <paramref name="source"/> was last known to be modified.</param>
        /// <param name="entityTag">The entity tag provided by the <paramref name="source"/> that is used to determine change in content.</param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
        /// <param name="response">
        ///     When this method returns, contains the <see cref="HttpWebResponse"/> for the supplied <paramref name="source"/>, if the web resource has been modified, or <b>null</b> if the web resource has <u>not</u> been modified. 
        ///     This parameter is passed uninitialized.
        /// </param>
        /// <returns><b>true</b> if the <paramref name="source"/> has been modified, otherwise <b>false</b>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        public static bool TryConditionalGet(Uri source, DateTime lastModified, string entityTag, WebRequestOptions options, out HttpWebResponse response)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            bool sourceHasBeenModified  = false;

            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(source, "source");

            //------------------------------------------------------------
            //	Initialize web request as a condtional get operation
            //------------------------------------------------------------
            HttpWebRequest httpRequest      = (HttpWebRequest)HttpWebRequest.Create(source);
            httpRequest.UserAgent           = frameworkUserAgent;
            httpRequest.IfModifiedSince     = lastModified;
            httpRequest.Headers.Add(HttpRequestHeader.IfNoneMatch, entityTag);
            if (options != null) options.ApplyOptions(httpRequest);

            //------------------------------------------------------------
            //	Perform conditional get operation
            //------------------------------------------------------------
            try
            {
                response    = (HttpWebResponse)httpRequest.GetResponse();

                if (DateTime.Compare(response.LastModified, lastModified) != 0)
                {
                    sourceHasBeenModified   = true;
                }
            }
            catch (WebException webException)
            {
                //------------------------------------------------------------
                //	Determine if response indicated resource was not modified
                //------------------------------------------------------------
                if (webException.Response != null && ((HttpWebResponse)webException.Response).StatusCode == HttpStatusCode.NotModified)
                {
                    sourceHasBeenModified   = false;
                    response                = null;
                }
                else
                {
                    //------------------------------------------------------------
                    //	Rethrow unexpected web exception
                    //------------------------------------------------------------
                    throw;
                }
            }

            return sourceHasBeenModified;
        }
Exemplo n.º 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="AtomFeed"/> 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="AtomFeed.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)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            XPathNavigator navigator    = null;

            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(source, "source");

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

            //------------------------------------------------------------
            //	Initialize XPathNavigator for supplied Uri, credentials, and proxy
            //------------------------------------------------------------
            if (settings.CharacterEncoding == 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
            //------------------------------------------------------------
            this.Load(navigator, settings, new SyndicationResourceLoadedEventArgs(navigator, source, options));
        }
Exemplo n.º 18
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="AtomFeed.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)
 {
     //------------------------------------------------------------
     //	Load syndication resource using default settings
     //------------------------------------------------------------
     this.Load(source, options, null);
 }
Exemplo n.º 19
0
        /// <summary>
        /// Creates a new <see cref="AtomFeed"/> instance using the specified <see cref="Uri"/>, <see cref="ICredentials"/>, <see cref="IWebProxy"/>, and <see cref="SyndicationResourceLoadSettings"/> object.
        /// </summary>
        /// <param name="source">A <see cref="Uri"/> that represents the URL of the syndication resource XML data.</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="AtomFeed"/> instance. This value can be <b>null</b>.</param>
        /// <returns>An <see cref="AtomFeed"/> object loaded using the <paramref name="source"/> data.</returns>
        /// <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>
        public static AtomFeed Create(Uri source, WebRequestOptions options, SyndicationResourceLoadSettings settings)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            AtomFeed syndicationResource = new AtomFeed();

            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(source, "source");

            //------------------------------------------------------------
            //	Create new instance using supplied parameters
            //------------------------------------------------------------
            syndicationResource.Load(source, options, settings);

            return syndicationResource;
        }
Exemplo n.º 20
0
 /// <summary>
 /// Creates a new <see cref="AtomFeed"/> instance using the specified <see cref="Uri"/>, <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="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
 /// <returns>An <see cref="AtomFeed"/> object loaded using the <paramref name="source"/> data.</returns>
 /// <remarks>
 ///     The <see cref="AtomFeed"/> is created using the default <see cref="SyndicationResourceLoadSettings"/>.
 /// </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>
 public static AtomFeed Create(Uri source, WebRequestOptions options)
 {
     //------------------------------------------------------------
     //	Create instance using supplied parameters and default settings
     //------------------------------------------------------------
     return AtomFeed.Create(source, options, null);
 }
 /// <summary>
 /// Creates a <see cref="XPathNavigator"/> against 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 XML data to be navigated by the created <see cref="XPathNavigator"/>.</param>
 /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
 /// <returns>
 ///     An <see cref="XPathNavigator"/> that provides a cursor model for navigating the supplied <paramref name="source"/>. 
 ///     The supplied <paramref name="source"/> XML data is parsed to remove invalid XML characters that would normally prevent 
 ///     a navigator from being created.
 /// </returns>
 /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
 public static XPathNavigator CreateSafeNavigator(Uri source, WebRequestOptions options)
 {
     //------------------------------------------------------------
     //	Create safe navigator using auto-detection of encoding
     //------------------------------------------------------------
     return SyndicationEncodingUtility.CreateSafeNavigator(source, options, null);
 }