by
10
7
3,708
3
Top 1% !
Popular
Famous
Nice
Easy-to-find
Specified
MultiPlatform
Popularity: 2986th place
Languagecsharp

kKlient.cs

an experiment in async task-based http methods          C#!!!!
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/662639/kKlient-cs?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
using System; using System.Net; using System.Web; using System.Net.Cache; using System.Threading; using System.Threading.Tasks; public class kKlient { public string Base { get; set; } public RequestCachePolicy CachPol { get; set; } public bool UseDef { get; set; } public ICredentials Credz { get; set; } public WebHeaderCollection Headz { get; set; } public IWebProxy Prxy { get; set; } public kKlient (object[] argz) { new HTTPAction(argz); } public async Task<string> FetchString (string uri, CancellationToken cToken = default (CancellationToken), IProgress<DownloadProgressChangedEventArgs> progress = null) { return await Task.Run(( ) => GetClient(cToken, progress).DownloadStringTaskAsync(uri)); } public async Task<byte[]> FetchData (string uri, CancellationToken cToken = default (CancellationToken), IProgress<DownloadProgressChangedEventArgs> progress = null) { return await Task.Run(( ) => GetClient(cToken, progress).DownloadDataTaskAsync(uri)); } public async Task FetchFile (string uri, string fileName, CancellationToken cToken = default (CancellationToken), IProgress<DownloadProgressChangedEventArgs> progress = null) { await Task.Run(( ) => GetClient(cToken, progress).DownloadFileTaskAsync(uri, fileName)); } internal class HTTPAction : WebClient { public HTTPAction ( ) : base() { } public HTTPAction (string url) : base() { } } private HTTPAction GetClient (CancellationToken cancelToken, IProgress<DownloadProgressChangedEventArgs> progress) { HTTPAction hta = new HTTPAction() { BaseAddress = Base, CachePolicy = CachPol, UseDefaultCredentials = UseDef, Credentials = Credz, Headers = Headz, Proxy = Prxy, }; if (cancelToken != CancellationToken.None) cancelToken.Register(( ) => hta.CancelAsync()); if (progress != null) { hta.DownloadProgressChanged += (sender, args) => progress.Report(args); } return hta; } }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus