working on it ...

Filters

Explore Public Snippets

Sort by

Found 51 snippets matching: webclient

    public by sTiLL-iLL @ SniPitz-KND  3716  3  7  10

    kKlient.cs

    an experiment in async task-based http methods C#!!!!
    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;
        }
    }           

    public by msdn  2312  15  7  0

    UploadStringTaskAsync: Uploads data in a string to the specified resource, asynchronously.

    Uploads data in a string to the specified resource, asynchronously. The WebClient. The URI to which the data should be uploaded. The HTTP method that should be used to upload the data. The data to upload. A Task containing the data in the response from the upload.
    using System;
    using System.IO;
    using System.Net;
    using System.Threading.Tasks;
    
    /// <summary>Uploads data in a string to the specified resource, asynchronously.</summary>
    /// <param name="webClient">The WebClient.</param>
    /// <param name="address">The URI to which the data should be uploaded.</param>
    /// <param name="method">The HTTP method that should be used to upload the data.</param>
    /// <param name="data">The data to upload.</param>
    /// <returns>A Task containing the data in the response from the upload.</returns>
    public static Task<string> UploadStringTaskAsync(this WebClient webClient, Uri address, string method, string data)
    {
        // Create the task to be returned
        var tcs = new TaskCompletionSource<string>(address);
    
        // Setup the callback event handler
        UploadStringCompletedEventHandler handler = null;
        handler = (sender, e) => EAPCommon.HandleCompletion(tcs, e, () => e.Result, ()=> webClient.UploadStringCompleted -= handler);
        webClient.UploadStringCompleted += handler;
    
        // Start the async work
        try
        {
            webClient.UploadStringAsync(address, method, data, tcs);
        }
        catch(Exception exc)
        {
            // If something goes wrong kicking off the async work,
            // unregister the callback and cancel the created task
            webClient.UploadStringCompleted -= handler;
            tcs.TrySetException(exc);
        }
    
        // Return the task that represents the async operation
        return tcs.Task;
    }

    public by msdn  1984  0  7  0

    UploadFileTask: Uploads a file to the specified resource, asynchronously.

    Uploads a file to the specified resource, asynchronously. The WebClient. The URI to which the file should be uploaded. The HTTP method that should be used to upload the file. A path to the file to upload. A Task containing the data in the response from the upload.
    using System.ComponentModel;
    using System.IO;
    using System.Threading.Tasks;
    
    /// <summary>Uploads a file to the specified resource, asynchronously.</summary>
    /// <param name="webClient">The WebClient.</param>
    /// <param name="address">The URI to which the file should be uploaded.</param>
    /// <param name="method">The HTTP method that should be used to upload the file.</param>
    /// <param name="fileName">A path to the file to upload.</param>
    /// <returns>A Task containing the data in the response from the upload.</returns>
    public static Task<byte[]> UploadFileTask(this WebClient webClient, Uri address, string method, string fileName)
    {
        // Create the task to be returned
        var tcs = new TaskCompletionSource<byte[]>(address);
    
        // Setup the callback event handler
        UploadFileCompletedEventHandler handler = null;
        handler = (sender, e) => EAPCommon.HandleCompletion(tcs, e, () => e.Result, () => webClient.UploadFileCompleted -= handler);
        webClient.UploadFileCompleted += handler;
    
        // Start the async work
        try
        {
            webClient.UploadFileAsync(address, method, fileName, tcs);
        }
        catch(Exception exc)
        {
            // If something goes wrong kicking off the async work,
            // unregister the callback and cancel the created task
            webClient.UploadFileCompleted -= handler;
            tcs.TrySetException(exc);
        }
    
        // Return the task that represents the async operation
        return tcs.Task;
    }

    public by msdn  1070  1  7  0

    OpenReadTaskAsync: Opens a readable stream for the data downloaded from a resource, asynchronously.

    Opens a readable stream for the data downloaded from a resource, asynchronously. The WebClient. The URI for which the stream should be opened. A Task that contains the opened stream.
    using System;
    using System.IO;
    using System.Net;
    using System.Threading.Tasks;
    
    /// <summary>Opens a readable stream for the data downloaded from a resource, asynchronously.</summary>
    /// <param name="webClient">The WebClient.</param>
    /// <param name="address">The URI for which the stream should be opened.</param>
    /// <returns>A Task that contains the opened stream.</returns>
    public static Task<Stream> OpenReadTaskAsync(this WebClient webClient, Uri address)
    {
        // Create the task to be returned
        var tcs = new TaskCompletionSource<Stream>(address);
    
        // Setup the callback event handler
        OpenReadCompletedEventHandler handler = null;
        handler = (sender, e) => EAPCommon.HandleCompletion(tcs, e, () => e.Result, () => webClient.OpenReadCompleted -= handler);
        webClient.OpenReadCompleted += handler;
    
        // Start the async work
        try
        {
            webClient.OpenReadAsync(address, tcs);
        }
        catch(Exception exc)
        {
            // If something goes wrong kicking off the async work,
            // unregister the callback and cancel the created task
            webClient.OpenReadCompleted -= handler;
            tcs.TrySetException(exc);
        }
    
        // Return the task that represents the async operation
        return tcs.Task;
    }

    public by msdn  875  0  6  0

    OpenWriteTaskAsync: Opens a writeable stream for uploading data to a resource, asynchronously.

    Opens a writeable stream for uploading data to a resource, asynchronously. The WebClient. The URI for which the stream should be opened. The HTTP method that should be used to open the stream. A Task that contains the opened stream.
    using System;
    using System.IO;
    using System.Net;
    using System.Threading.Tasks;
    
    /// <summary>Opens a writeable stream for uploading data to a resource, asynchronously.</summary>
    /// <param name="webClient">The WebClient.</param>
    /// <param name="address">The URI for which the stream should be opened.</param>
    /// <param name="method">The HTTP method that should be used to open the stream.</param>
    /// <returns>A Task that contains the opened stream.</returns>
    public static Task<Stream> OpenWriteTaskAsync(this WebClient webClient, Uri address, string method)
    {
        // Create the task to be returned
        var tcs = new TaskCompletionSource<Stream>(address);
    
        // Setup the callback event handler
        OpenWriteCompletedEventHandler handler = null;
        handler = (sender, e) => EAPCommon.HandleCompletion(tcs, e, () => e.Result, () => webClient.OpenWriteCompleted -= handler);
        webClient.OpenWriteCompleted += handler;
    
        // Start the async work
        try
        {
            webClient.OpenWriteAsync(address, method, tcs);
        }
        catch(Exception exc)
        {
            // If something goes wrong kicking off the async work,
            // unregister the callback and cancel the created task
            webClient.OpenWriteCompleted -= handler;
            tcs.TrySetException(exc);
        }
                
        // Return the task that represents the async operation
        return tcs.Task;
    }

    public by msdn  752  0  6  0

    DownloadStringTaskAsync: Downloads the resource with the specified URI as a string, asynchronously.

    Downloads the resource with the specified URI as a string, asynchronously. The WebClient. The URI from which to download data. A Task that contains the downloaded string.
    using System;
    using System.IO;
    using System.Net;
    using System.Threading.Tasks;
    
    /// <summary>Downloads the resource with the specified URI as a string, asynchronously.</summary>
    /// <param name="webClient">The WebClient.</param>
    /// <param name="address">The URI from which to download data.</param>
    /// <returns>A Task that contains the downloaded string.</returns>
    public static Task<string> DownloadStringTaskAsync(this WebClient webClient, Uri address)
    {
        // Create the task to be returned
        var tcs = new TaskCompletionSource<string>(address);
                
        // Setup the callback event handler
        DownloadStringCompletedEventHandler handler = null;
        handler = (sender, e) => EAPCommon.HandleCompletion(tcs, e, () => e.Result, () => webClient.DownloadStringCompleted -= handler);
        webClient.DownloadStringCompleted += handler;
    
        // Start the async work
        try
        {
            webClient.DownloadStringAsync(address, tcs);
        }
        catch(Exception exc)
        {
            // If something goes wrong kicking off the async work,
            // unregister the callback and cancel the created task
            webClient.DownloadStringCompleted -= handler;
            tcs.TrySetException(exc);
        }
    
        // Return the task that represents the async operation
        return tcs.Task;
    }

    public by msdn  747  0  6  0

    UploadStringTask: Uploads data in a string to the specified resource, asynchronously.

    Uploads data in a string to the specified resource, asynchronously. The WebClient. The URI to which the data should be uploaded. The HTTP method that should be used to upload the data. The data to upload. A Task containing the data in the response from the upload.
    using System.ComponentModel;
    using System.IO;
    using System.Threading.Tasks;
    
    /// <summary>Uploads data in a string to the specified resource, asynchronously.</summary>
    /// <param name="webClient">The WebClient.</param>
    /// <param name="address">The URI to which the data should be uploaded.</param>
    /// <param name="method">The HTTP method that should be used to upload the data.</param>
    /// <param name="data">The data to upload.</param>
    /// <returns>A Task containing the data in the response from the upload.</returns>
    public static Task<string> UploadStringTask(this WebClient webClient, Uri address, string method, string data)
    {
        // Create the task to be returned
        var tcs = new TaskCompletionSource<string>(address);
    
        // Setup the callback event handler
        UploadStringCompletedEventHandler handler = null;
        handler = (sender, e) => EAPCommon.HandleCompletion(tcs, e, () => e.Result, ()=> webClient.UploadStringCompleted -= handler);
        webClient.UploadStringCompleted += handler;
    
        // Start the async work
        try
        {
            webClient.UploadStringAsync(address, method, data, tcs);
        }
        catch(Exception exc)
        {
            // If something goes wrong kicking off the async work,
            // unregister the callback and cancel the created task
            webClient.UploadStringCompleted -= handler;
            tcs.TrySetException(exc);
        }
    
        // Return the task that represents the async operation
        return tcs.Task;
    }

    public by msdn  684  0  6  0

    UploadDataTask: Uploads data to the specified resource, asynchronously.

    Uploads data to the specified resource, asynchronously. The WebClient. The URI to which the data should be uploaded. The HTTP method that should be used to upload the data. The data to upload. A Task containing the data in the response from the upload.
    using System.ComponentModel;
    using System.IO;
    using System.Threading.Tasks;
    
    /// <summary>Uploads data to the specified resource, asynchronously.</summary>
    /// <param name="webClient">The WebClient.</param>
    /// <param name="address">The URI to which the data should be uploaded.</param>
    /// <param name="method">The HTTP method that should be used to upload the data.</param>
    /// <param name="data">The data to upload.</param>
    /// <returns>A Task containing the data in the response from the upload.</returns>
    public static Task<byte[]> UploadDataTask(this WebClient webClient, Uri address, string method, byte [] data)
    {
        // Create the task to be returned
        var tcs = new TaskCompletionSource<byte[]>(address);
    
        // Setup the callback event handler
        UploadDataCompletedEventHandler handler = null;
        handler = (sender, e) => EAPCommon.HandleCompletion(tcs, e, () => e.Result, () => webClient.UploadDataCompleted -= handler);
        webClient.UploadDataCompleted += handler;
    
        // Start the async work
        try
        {
            webClient.UploadDataAsync(address, method, data, tcs);
        }
        catch(Exception exc)
        {
            // If something goes wrong kicking off the async work,
            // unregister the callback and cancel the created task
            webClient.UploadDataCompleted -= handler;
            tcs.TrySetException(exc);
        }
    
        // Return the task that represents the async operation
        return tcs.Task;
    }

    public by msdn  646  0  6  0

    DownloadDataTask: Downloads the resource with the specified URI as a byte array, asynchronously.

    Downloads the resource with the specified URI as a byte array, asynchronously. The WebClient. The URI from which to download data. A Task that contains the downloaded data.
    using System.ComponentModel;
    using System.IO;
    using System.Threading.Tasks;
    
    /// <summary>Downloads the resource with the specified URI as a byte array, asynchronously.</summary>
    /// <param name="webClient">The WebClient.</param>
    /// <param name="address">The URI from which to download data.</param>
    /// <returns>A Task that contains the downloaded data.</returns>
    public static Task<byte[]> DownloadDataTask(this WebClient webClient, Uri address)
    {
        // Create the task to be returned
        var tcs = new TaskCompletionSource<byte[]>(address);
    
        // Setup the callback event handler
        DownloadDataCompletedEventHandler handler = null;
        handler = (sender, e) => EAPCommon.HandleCompletion(tcs, e, () => e.Result, () => webClient.DownloadDataCompleted -= handler);
        webClient.DownloadDataCompleted += handler;
    
        // Start the async work
        try
        {
            webClient.DownloadDataAsync(address, tcs);
        }
        catch(Exception exc)
        {
            // If something goes wrong kicking off the async work,
            // unregister the callback and cancel the created task
            webClient.DownloadDataCompleted -= handler;
            tcs.TrySetException(exc);
        }
    
        // Return the task that represents the async operation
        return tcs.Task;
    }

    public by msdn  636  0  6  0

    OpenWriteTask: Opens a writeable stream for uploading data to a resource, asynchronously.

    Opens a writeable stream for uploading data to a resource, asynchronously. The WebClient. The URI for which the stream should be opened. The HTTP method that should be used to open the stream. A Task that contains the opened stream.
    using System.ComponentModel;
    using System.IO;
    using System.Threading.Tasks;
    
    /// <summary>Opens a writeable stream for uploading data to a resource, asynchronously.</summary>
    /// <param name="webClient">The WebClient.</param>
    /// <param name="address">The URI for which the stream should be opened.</param>
    /// <param name="method">The HTTP method that should be used to open the stream.</param>
    /// <returns>A Task that contains the opened stream.</returns>
    public static Task<Stream> OpenWriteTask(this WebClient webClient, Uri address, string method)
    {
        // Create the task to be returned
        var tcs = new TaskCompletionSource<Stream>(address);
    
        // Setup the callback event handler
        OpenWriteCompletedEventHandler handler = null;
        handler = (sender, e) => EAPCommon.HandleCompletion(tcs, e, () => e.Result, () => webClient.OpenWriteCompleted -= handler);
        webClient.OpenWriteCompleted += handler;
    
        // Start the async work
        try
        {
            webClient.OpenWriteAsync(address, method, tcs);
        }
        catch(Exception exc)
        {
            // If something goes wrong kicking off the async work,
            // unregister the callback and cancel the created task
            webClient.OpenWriteCompleted -= handler;
            tcs.TrySetException(exc);
        }
                
        // Return the task that represents the async operation
        return tcs.Task;
    }
    • Public Snippets
    • Channels Snippets