private static async Task <XmlDocument> SimpleRequest(string method, HttpAsyncRequestFilter filter, XmlRequestResult result, params string[] parameters)
        {
            string absUri = UrlHelper.SafeToAbsoluteUri(result.uri);

            if (parameters.Length > 0)
            {
                FormData formData = new FormData(true, parameters);

                if (absUri.IndexOf('?') == -1)
                {
                    absUri += "?" + formData.ToString();
                }
                else
                {
                    absUri += "&" + formData.ToString();
                }
            }

            RedirectHelper.SimpleRequest simpleRequest = new RedirectHelper.SimpleRequest(method, filter);
            var response = await RedirectHelper.GetResponse(absUri, new RedirectHelper.RequestFactory(simpleRequest.Create));

            try
            {
                result.uri             = response.RequestMessage.RequestUri;
                result.responseHeaders = response.Headers;
                return(await ParseXmlResponse(response));
            }
            finally
            {
                if (response != null)
                {
                    response.Dispose();
                }
            }
        }
Ejemplo n.º 2
0
        /// <param name="uri"></param>
        /// <param name="methods">An array of HTTP methods that should be tried until one of them does not return 405.</param>
        private static async Task <string> GetEtagImpl(string uri, HttpAsyncRequestFilter requestFilter, params string[] methods)
        {
            try
            {
                var response = await RedirectHelper.GetResponse(uri,
                                                                new RedirectHelper.RequestFactory(new RedirectHelper.SimpleRequest(methods[0], requestFilter).Create));

                try
                {
                    return(FilterWeakEtag(response.Headers["ETag"]));
                }
                finally
                {
                    response.Dispose();
                }
            }
            catch (WebException we)
            {
                if (methods.Length > 1 && we.Status == WebExceptionStatus.ProtocolError)
                {
                    HttpWebResponse resp = we.Response as HttpWebResponse;
                    if (resp != null && (resp.StatusCode == HttpStatusCode.MethodNotAllowed || resp.StatusCode == HttpStatusCode.NotImplemented))
                    {
                        string[] newMethods = new string[methods.Length - 1];
                        Array.Copy(methods, 1, newMethods, 0, newMethods.Length);
                        return(await GetEtagImpl(uri, requestFilter, newMethods));
                    }
                }
                throw;
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Create an XmlRpcClient for the specified host name
 /// </summary>
 /// <param name="hostname"></param>
 public XmlRpcClient(string hostname, string userAgent, HttpAsyncRequestFilter filter, string transportEncoding)
 {
     _hostname          = hostname;
     _userAgent         = userAgent;
     _requestFilter     = filter;
     _transportEncoding = transportEncoding;
 }
 public AtomMediaUploader(XmlNamespaceManager nsMgr, HttpAsyncRequestFilter requestFilter, string collectionUri, IBlogClientOptions options, XmlRestRequestHelper xmlRestRequestHelper)
 {
     this._nsMgr               = nsMgr;
     this._requestFilter       = requestFilter;
     this._collectionUri       = collectionUri;
     this._options             = options;
     this.xmlRestRequestHelper = xmlRestRequestHelper;
 }
            public SendFactory(string etag, string method, HttpAsyncRequestFilter filter, string contentType, XmlDocument doc, string encoding)
            {
                _etag        = etag;
                _method      = method;
                _filter      = filter;
                _contentType = contentType;
                _doc         = doc;

                //select the encoding
                _encodingToUse = new UTF8Encoding(false, false);
                try
                {
                    _encodingToUse = Encoding.GetEncoding(encoding);
                }
                catch (Exception ex)
                {
                    //Debug.Fail("Error while getting transport encoding: " + ex.ToString());
                }
            }
        protected virtual async Task <XmlDocument> Send(string method, string etag, HttpAsyncRequestFilter filter, string contentType, XmlDocument doc, string encoding, string filename, bool ignoreResponse, XmlRequestResult result)
        {
            if (!String.IsNullOrEmpty(filename))
            {
                return(MultipartSend(method, etag, filter, contentType, doc, encoding, filename, ignoreResponse,
                                     result));
            }

            string absUri = UrlHelper.SafeToAbsoluteUri(result.uri);

            Debug.WriteLine("XML Request to " + absUri + ":\r\n" + doc.GetXml());

            SendFactory sf       = new SendFactory(etag, method, filter, contentType, doc, encoding);
            var         response = await RedirectHelper.GetResponse(absUri, new RedirectHelper.RequestFactory(sf.Create));

            try
            {
                result.responseHeaders = response.Headers;
                result.uri             = response.RequestMessage.RequestUri;
                if (ignoreResponse || response.StatusCode == Windows.Web.Http.HttpStatusCode.NoContent)
                {
                    return(null);
                }
                else
                {
                    XmlDocument xmlDocResponse = await ParseXmlResponse(response);

                    return(xmlDocResponse);
                }
            }
            finally
            {
                if (response != null)
                {
                    response.Dispose();
                }
            }
        }
            public MultipartMimeSendFactory(HttpAsyncRequestFilter filter, XmlDocument xmlRequest, string filename, string encoding, MultipartMimeRequestHelper multipartMimeRequestHelper)
            {
                if (xmlRequest == null)
                {
                    throw new ArgumentNullException();
                }

                // Add boundary to params
                _filename = filename;
                _xmlDoc   = xmlRequest;
                _filter   = filter;
                _multipartMimeRequestHelper = multipartMimeRequestHelper;

                //select the encoding
                _encoding = new UTF8Encoding(false, false);
                try
                {
                    _encoding = Encoding.GetEncoding(encoding);
                }
                catch (Exception ex)
                {
                    //Debug.Fail("Error while getting transport encoding: " + ex.ToString());
                }
            }
 public async Task <HttpResponseMessage> SendAuthenticatedHttpRequest(string requestUri, int timeoutMs, HttpAsyncRequestFilter filter)
 {
     return(await BlogClientHelper.SendAuthenticatedHttpRequest(requestUri, filter, await CreateAuthorizationFilter()));
 }
 public AtomMediaUploader(XmlNamespaceManager nsMgr, HttpAsyncRequestFilter requestFilter, string collectionUri, IBlogClientOptions options)
     : this(nsMgr, requestFilter, collectionUri, options, new XmlRestRequestHelper())
 {
 }
        public async static Task <HttpResponseMessage> SendRequest(string requestUri, HttpAsyncRequestFilter filter)
        {
            var httpClient = new HttpClient();


            var request = CreateHttpWebRequest(requestUri, true, null, null);

            if (filter != null)
            {
                await filter(request);
            }

            // get the response
            try
            {
                var response = await httpClient.SendRequestAsync(request);

                //hack: For some reason, disabling auto-redirects also disables throwing WebExceptions for 300 status codes,
                //so if we detect a non-2xx error code here, throw a web exception.
                response.EnsureSuccessStatusCode();
                return(response);
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.Timeout)
                {
                    //throw a typed exception that lets callers know that the response timed out after the request was sent
                    throw new WebResponseTimeoutException(e);
                }
                else
                {
                    throw;
                }
            }
        }
Ejemplo n.º 11
0
        public static Task <HttpResponseMessage> SendAuthenticatedHttpRequest(string requestUri, HttpAsyncRequestFilter filter, HttpAsyncRequestFilter credentialsFilter)
        {
            // build filter list
            ArrayList filters = new ArrayList();

            if (filter != null)
            {
                filters.Add(filter);
            }
            if (credentialsFilter != null)
            {
                filters.Add(credentialsFilter);
            }

            // send request
            try
            {
                return(HttpRequestHelper.SendRequest(
                           requestUri,
                           CompoundHttpRequestFilter.Create(filters.ToArray(typeof(HttpAsyncRequestFilter)) as HttpAsyncRequestFilter[])));
            }
            catch (BlogClientOperationCancelledException)
            {
                // if we are in silent mode and  failed to authenticate then try again w/o requiring auth
                //if ( BlogClientUIContext.SilentModeForCurrentThread )
                //{
                //	return HttpRequestHelper.SendRequest( requestUri, filter ) ;
                //}
                //else
                //{
                throw;
                //}
            }
        }
Ejemplo n.º 12
0
 public virtual async Task <HttpResponseMessage> SendAuthenticatedHttpRequest(string requestUri, int timeoutMs, HttpAsyncRequestFilter filter)
 {
     return(await BlogClientHelper.SendAuthenticatedHttpRequest(requestUri, filter, await CreateCredentialsFilter(requestUri)));
 }
 public async static Task <HttpResponseMessage> SafeSendRequest(string requestUri, HttpAsyncRequestFilter filter)
 {
     try
     {
         return(await SendRequest(requestUri, filter));
     }
     catch (WebException we)
     {
         //if (ApplicationDiagnostics.TestMode)
         //	LogException(we);
         return(null);
     }
 }
 /// <summary>
 /// Performs an HTTP DELETE on the URL and contains no body, returns the body as an XmlDocument if there is one
 /// </summary>
 public virtual Task <XmlDocument> Delete(HttpAsyncRequestFilter filter, XmlRequestResult result)
 {
     return(SimpleRequest("DELETE", filter, result, new string[] { }));
 }
Ejemplo n.º 15
0
 public static async Task <string> GetEtag(string uri, HttpAsyncRequestFilter requestFilter)
 {
     return(await GetEtagImpl(uri, requestFilter, "HEAD", "GET"));
 }
 /// <summary>
 /// Performs an HTTP PUT with the specified XML document as the request body.
 /// </summary>
 public Task <XmlDocument> Put(string etag, HttpAsyncRequestFilter filter, string contentType, XmlDocument doc, string encoding, bool ignoreResponse, XmlRequestResult result)
 {
     return(Send("PUT", etag, filter, contentType, doc, encoding, null, ignoreResponse, result));
 }
 /// <summary>
 /// Performs a multipart MIME HTTP POST with the specified XML document as the request body and filename as the payload.
 /// </summary>
 public Task <XmlDocument> Post(HttpAsyncRequestFilter filter, string contentType, XmlDocument doc, string encoding, string filename, XmlRequestResult result)
 {
     return(Send("POST", null, filter, contentType, doc, encoding, filename, false, result));
 }
 protected virtual XmlDocument MultipartSend(string method, string etag, HttpAsyncRequestFilter filter, string contentType, XmlDocument doc, string encoding, string filename, bool ignoreResponse, XmlRequestResult result)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Retrieve the specified URI, using the given filter, with the supplied parameters (if any).
 /// The parameters parameter should be an even number of strings, where each odd element is
 /// a param name and each following even element is the corresponding param value.  For example,
 /// to retrieve http://www.vox.com/atom?svc=post&id=100, you would say:
 ///
 /// Get("http://www.vox.com/atom", "svc", "post", "id", "100");
 ///
 /// If a param value is null or empty string, that param will not be included in the final URL
 /// (i.e. the corresponding param name will also be dropped).
 /// </summary>
 public virtual Task <XmlDocument> Get(HttpAsyncRequestFilter filter, XmlRequestResult result, params string[] parameters)
 {
     return(SimpleRequest("GET", filter, result, parameters));
 }
Ejemplo n.º 20
0
 public SimpleRequest(string method, HttpAsyncRequestFilter filter)
 {
     _method = method;
     _filter = filter;
 }