Esempio n. 1
0
        /// <summary>Invoked when a request is completed.</summary>
        /// <param name="state">The asynchronous state.</param>
        private static void OnRequestCompleted(IAsyncResult state)
        {
            byte[]               data  = null;
            Exception            error = null;
            BinaryWebClientState bwcs  = state.AsyncState as BinaryWebClientState;

            if (bwcs.Request != null)
            {
                try { data = ReadResponse(bwcs.Request); }
                catch (WebException e) { error = e; }
            }

            if (bwcs.Callback != null)
            {
                bwcs.Callback(data, bwcs.State, error);
            }
        }
Esempio n. 2
0
        /// <summary>Invoked when a request is completed.</summary>
        /// <param name="state">The asynchronous state.</param>
        private static void OnRequestStreamObtained(IAsyncResult state)
        {
            BinaryWebClientState bwcs = state.AsyncState as BinaryWebClientState;

            if (bwcs.Request != null)
            {
                using (Stream stream = bwcs.Request.EndGetRequestStream(state))
                {
                    byte[] buffer = new byte[bwcs.Data.Length];
                    Array.Copy(bwcs.Data, buffer, buffer.Length);

                    stream.Write(buffer, 0, buffer.Length);
                    stream.Flush();

                    Array.Clear(buffer, 0, buffer.Length);
                    buffer = null;
                }

                bwcs.Request.BeginGetResponse(OnRequestCompleted, bwcs);
            }
        }