static void OpenWrite(IAsyncResult ar) { RequestState state = (RequestState)ar.AsyncState; try { // Get the stream to write our upload to using (Stream uploadStream = state.Request.EndGetRequestStream(ar)) { // Fire the callback for successfully opening the stream if (state.OpenWriteCallback != null) { state.OpenWriteCallback(state.Request); } // Write our data to the upload stream uploadStream.Write(state.UploadData, 0, state.UploadData.Length); } // Start the request for the remote server response IAsyncResult result = state.Request.BeginGetResponse(GetResponse, state); // Register a timeout for the request ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, TimeoutCallback, state, state.MillisecondsTimeout, true); } catch (Exception ex) { //Logger.Log.Debug("CapsBase.OpenWrite(): " + ex.Message); if (state.CompletedCallback != null) { state.CompletedCallback(state.Request, null, null, ex); } } }
public static HttpWebRequest UploadDataAsync(Uri address, X509Certificate2 clientCert, string contentType, byte[] data, int millisecondsTimeout, OpenWriteEventHandler openWriteCallback, DownloadProgressEventHandler downloadProgressCallback, RequestCompletedEventHandler completedCallback) { // Create the request HttpWebRequest request = SetupRequest(address, clientCert); request.ContentLength = data.Length; if (!String.IsNullOrEmpty(contentType)) { request.ContentType = contentType; } request.Method = "POST"; // Create an object to hold all of the state for this request RequestState state = new RequestState(request, data, millisecondsTimeout, openWriteCallback, downloadProgressCallback, completedCallback); // Start the request for a stream to upload to IAsyncResult result = request.BeginGetRequestStream(OpenWrite, state); // Register a timeout for the request ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, TimeoutCallback, state, millisecondsTimeout, true); return(request); }
public static void DownloadDataAsync(HttpWebRequest request, int millisecondsTimeout, DownloadProgressEventHandler downloadProgressCallback, RequestCompletedEventHandler completedCallback) { // Create an object to hold all of the state for this request RequestState state = new RequestState(request, null, millisecondsTimeout, null, downloadProgressCallback, completedCallback); // Start the request for the remote server response IAsyncResult result = request.BeginGetResponse(GetResponse, state); // Register a timeout for the request ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, TimeoutCallback, state, millisecondsTimeout, true); }