Пример #1
0
        /// <summary>
        /// Sends a YADIS HTTP request as part of identifier discovery.
        /// </summary>
        /// <param name="requestHandler">The request handler to use to actually submit the request.</param>
        /// <param name="uri">The URI to GET.</param>
        /// <param name="requireSsl">Whether only HTTPS URLs should ever be retrieved.</param>
        /// <param name="acceptTypes">The value of the Accept HTTP header to include in the request.</param>
        /// <returns>The HTTP response retrieved from the request.</returns>
        internal static IncomingWebResponse Request(IDirectWebRequestHandler requestHandler, Uri uri, bool requireSsl, params string[] acceptTypes)
        {
            Requires.NotNull(requestHandler, "requestHandler");
            Requires.NotNull(uri, "uri");
            Contract.Ensures(Contract.Result <IncomingWebResponse>() != null);
            Contract.Ensures(Contract.Result <IncomingWebResponse>().ResponseStream != null);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

            request.CachePolicy = IdentifierDiscoveryCachePolicy;
            if (acceptTypes != null)
            {
                request.Accept = string.Join(",", acceptTypes);
            }

            DirectWebRequestOptions options = DirectWebRequestOptions.None;

            if (requireSsl)
            {
                options |= DirectWebRequestOptions.RequireSsl;
            }

            try {
                return(requestHandler.GetResponse(request, options));
            } catch (ProtocolException ex) {
                var webException = ex.InnerException as WebException;
                if (webException != null)
                {
                    var response = webException.Response as HttpWebResponse;
                    if (response != null && response.IsFromCache)
                    {
                        // We don't want to report error responses from the cache, since the server may have fixed
                        // whatever was causing the problem.  So try again with cache disabled.
                        Logger.Messaging.Error("An HTTP error response was obtained from the cache.  Retrying with cache disabled.", ex);
                        var nonCachingRequest = request.Clone();
                        nonCachingRequest.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Reload);
                        return(requestHandler.GetResponse(nonCachingRequest, options));
                    }
                }

                throw;
            }
        }
Пример #2
0
        public DirectWebResponse GetResponse(HttpWebRequest request, DirectWebRequestOptions options)
        {
            ErrorUtilities.VerifyArgumentNotNull(request, "request");

            // This request MAY have already been prepared by GetRequestStream, but
            // we have no guarantee, so do it just to be safe.
            this.PrepareRequest(request, false);

            // Since we may require SSL for every redirect, we handle each redirect manually
            // in order to detect and fail if any redirect sends us to an HTTP url.
            // We COULD allow automatic redirect in the cases where HTTPS is not required,
            // but our mock request infrastructure can't do redirects on its own either.
            Uri originalRequestUri = request.RequestUri;
            int i;

            for (i = 0; i < this.MaximumRedirections; i++)
            {
                this.EnsureAllowableRequestUri(request.RequestUri, (options & DirectWebRequestOptions.RequireSsl) != 0);
                CachedDirectWebResponse response = this.chainedWebRequestHandler.GetResponse(request, options & ~DirectWebRequestOptions.RequireSsl).GetSnapshot(this.MaximumBytesToRead);
                if (response.Status == HttpStatusCode.MovedPermanently ||
                    response.Status == HttpStatusCode.Redirect ||
                    response.Status == HttpStatusCode.RedirectMethod ||
                    response.Status == HttpStatusCode.RedirectKeepVerb)
                {
                    // We have no copy of the post entity stream to repeat on our manually
                    // cloned HttpWebRequest, so we have to bail.
                    ErrorUtilities.VerifyProtocol(request.Method != "POST", MessagingStrings.UntrustedRedirectsOnPOSTNotSupported);
                    Uri redirectUri = new Uri(response.FinalUri, response.Headers[HttpResponseHeader.Location]);
                    request = request.Clone(redirectUri);
                }
                else
                {
                    return(response);
                }
            }

            throw ErrorUtilities.ThrowProtocol(MessagingStrings.TooManyRedirects, originalRequestUri);
        }
Пример #3
0
        public string POST(string URL, Dictionary <string, string> form)
        {
            HttpWebRequest request = this.defaultReq(urls[URL]);

            request.Accept      = "application/json, text/javascript, */*; q=0.01";
            request.Method      = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            string response = null;

            do
            {
                HttpWebRequest req = request.Clone();
                form[form.Keys.Single(k => k.Contains("token"))] = this.GetToken(tokens[URL]);
                req.Referer = this.referer;
                StringBuilder sb = new StringBuilder();
                foreach (var prm in form)
                {
                    if (sb.Length != 0)
                    {
                        sb.Append("&");
                    }
                    sb.Append(HttpUtility.UrlEncode(prm.Key));
                    sb.Append("=");
                    sb.Append(HttpUtility.UrlEncode(prm.Value));
                }

                var data = Encoding.ASCII.GetBytes(sb.ToString());
                request.ContentLength = data.Length;
                using (var stream = req.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }

                response = new StreamReader(((HttpWebResponse)request.GetResponse()).GetResponseStream()).ReadToEnd();
            } while (response.Contains("error"));
            return(response);
        }