/// <summary> /// Asynchronously downloads an object (streaming) from the specified url. /// </summary> /// <param name="url">The URL.</param> /// <param name="acceptEncoded">if set to <c>true</c> accept encoded response.</param> /// <param name="postData">The post data. If null, GET will be used.</param> /// <param name="parser">The function which will parse the stream.</param> /// <param name="token">The ESI token, or null if none is used.</param> public static async Task <DownloadResult <T> > DownloadStreamAsync <T>(Uri url, ParseDataDelegate <T> parser, bool acceptEncoded = false, HttpPostData postData = null, string token = null) { string urlValidationError; if (!IsValidURL(url, out urlValidationError)) { throw new ArgumentException(urlValidationError); } HttpClientServiceRequest request = new HttpClientServiceRequest(); request.AuthToken = token; request.AcceptEncoded = acceptEncoded; request.DataCompression = postData?.Compression ?? DataCompression.None; try { var response = await request.SendAsync(url, (postData == null)? HttpMethod.Get : HttpMethod.Post, postData, StreamAccept). ConfigureAwait(false); using (response) { var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); return(GetResult(url, stream, parser, response)); } } catch (HttpWebClientServiceException ex) { return(new DownloadResult <T>(default(T), ex)); } }
/// <summary> /// Asynchronously downloads a string from the specified url. /// </summary> /// <param name="url">The URL.</param> /// <param name="method">The method.</param> /// <param name="acceptEncoded">if set to <c>true</c> accept encoded response.</param> /// <param name="postdata">The post data.</param> /// <param name="dataCompression">The post data compression method.</param> public static async Task<DownloadResult<String>> DownloadStringAsync(Uri url, HttpMethod method = null, bool acceptEncoded = false, string postdata = null, DataCompression dataCompression = DataCompression.None) { string urlValidationError; if (!IsValidURL(url, out urlValidationError)) throw new ArgumentException(urlValidationError); HttpPostData postData = String.IsNullOrWhiteSpace(postdata) ? null : new HttpPostData(postdata, dataCompression); HttpClientServiceRequest request = new HttpClientServiceRequest(); try { HttpResponseMessage response = await request.SendAsync(url, method, postData, dataCompression, acceptEncoded, StringAccept).ConfigureAwait(false); using (response) { Stream stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); return GetString(request.BaseUrl, stream); } } catch (HttpWebClientServiceException ex) { return new DownloadResult<String>(String.Empty, ex); } }
/// <summary> /// Asynchronously downloads an xml file from the specified url. /// </summary> /// <param name="url">The URL.</param> /// <param name="method">The method.</param> /// <param name="acceptEncoded">if set to <c>true</c> accept encoded response.</param> /// <param name="postdata">The post data.</param> /// <param name="dataCompression">The post data compression method.</param> public static async Task <DownloadResult <IXPathNavigable> > DownloadXmlAsync(Uri url, HttpMethod method = null, bool acceptEncoded = false, string postdata = null, DataCompression dataCompression = DataCompression.None) { string urlValidationError; if (!IsValidURL(url, out urlValidationError)) { throw new ArgumentException(urlValidationError); } HttpPostData postData = string.IsNullOrWhiteSpace(postdata) ? null : new HttpPostData(postdata, dataCompression); HttpClientServiceRequest request = new HttpClientServiceRequest(); request.AuthToken = null; request.AcceptEncoded = acceptEncoded; request.DataCompression = dataCompression; try { HttpResponseMessage response = await request.SendAsync(url, method, postData, XmlAccept).ConfigureAwait(false); using (response) { Stream stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); return(GetXmlDocument(request.BaseUrl, stream, response)); } } catch (HttpWebClientServiceException ex) { return(new DownloadResult <IXPathNavigable>(new XmlDocument(), ex)); } }
/// <summary> /// Asynchronously downloads an image from the specified url. /// </summary> /// <param name="url">The URL.</param> /// <param name="param">The request parameters. If null, defaults will be used.</param> public static async Task <DownloadResult <Image> > DownloadImageAsync(Uri url, RequestParams param = null) { string urlValidationError; if (!IsValidURL(url, out urlValidationError)) { throw new ArgumentException(urlValidationError); } var request = new HttpClientServiceRequest(); try { var response = await request.SendAsync(url, param, ImageAccept). ConfigureAwait(false); using (response) { Stream stream = await response.Content.ReadAsStreamAsync(). ConfigureAwait(false); return(GetImage(request.BaseUrl, stream, response)); } } catch (HttpWebClientServiceException ex) { return(new DownloadResult <Image>(null, ex)); } }
/// <summary> /// Asynchronously downloads an object (streaming) from the specified url. /// </summary> /// <param name="url">The URL.</param> /// <param name="parser">The function which will parse the stream.</param> /// <param name="param">The request parameters. If null, defaults will be used.</param> public static async Task <DownloadResult <T> > DownloadStreamAsync <T>(Uri url, ParseDataDelegate <T> parser, RequestParams param) { string urlValidationError; if (!IsValidURL(url, out urlValidationError)) { throw new ArgumentException(urlValidationError); } var request = new HttpClientServiceRequest(); try { var response = await request.SendAsync(url, param, StreamAccept). ConfigureAwait(false); using (response) { var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait( false); return(GetResult(url, stream, parser, response)); } } catch (HttpWebClientServiceException ex) { return(new DownloadResult <T>(default(T), ex)); } }
/// <summary> /// Asynchronously downloads a string from the specified url. /// </summary> /// <param name="url">The URL.</param> /// <param name="method">The method.</param> /// <param name="acceptEncoded">if set to <c>true</c> accept encoded response.</param> /// <param name="postdata">The post data.</param> /// <param name="dataCompression">The post data compression method.</param> public static async Task <DownloadResult <String> > DownloadStringAsync(Uri url, HttpMethod method = null, bool acceptEncoded = false, string postdata = null, DataCompression dataCompression = DataCompression.None) { string urlValidationError; if (!IsValidURL(url, out urlValidationError)) { throw new ArgumentException(urlValidationError); } HttpPostData postData = String.IsNullOrWhiteSpace(postdata) ? null : new HttpPostData(postdata, dataCompression); HttpClientServiceRequest request = new HttpClientServiceRequest(); try { HttpResponseMessage response = await request.SendAsync(url, method, postData, dataCompression, acceptEncoded, StringAccept).ConfigureAwait(false); using (response) { Stream stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); return(GetString(request.BaseUrl, stream)); } } catch (HttpWebClientServiceException ex) { return(new DownloadResult <String>(String.Empty, ex)); } }
/// <summary> /// Gets the web client. /// </summary> /// <returns></returns> public static WebClient GetWebClient() => new WebClient { Proxy = HttpClientServiceRequest.GetWebProxy() };