Пример #1
0
        /// <devdoc>
        ///    <para>Generates a byte array or downloads data to an open file stream</para>
        /// </devdoc>
        private byte[] DownloadBits(WebRequest request, Stream writeStream, CompletionDelegate completionDelegate, AsyncOperation asyncOp) {
            WebResponse response = null;
            DownloadBitsState state = new DownloadBitsState(request, writeStream, completionDelegate, asyncOp, m_Progress, this);

            if (state.Async) {
                request.BeginGetResponse(new AsyncCallback(DownloadBitsResponseCallback), state);
                return null;
            } else {
                response = m_WebResponse = GetWebResponse(request);
            }

            bool completed;
            int bytesRead = state.SetResponse(response);
            do {
                completed = state.RetrieveBytes(ref bytesRead);
            } while (!completed);
            state.Close();
            return state.InnerBuffer;
        }
 private static void DownloadBitsReadCallbackState(DownloadBitsState state, IAsyncResult result)
 {
     Stream readStream = state.ReadStream;
     Exception exception = null;
     bool flag = false;
     try
     {
         int bytesRetrieved = 0;
         if ((readStream != null) && (readStream != Stream.Null))
         {
             bytesRetrieved = readStream.EndRead(result);
         }
         flag = state.RetrieveBytes(ref bytesRetrieved);
     }
     catch (Exception exception2)
     {
         flag = true;
         if (((exception2 is ThreadAbortException) || (exception2 is StackOverflowException)) || (exception2 is OutOfMemoryException))
         {
             throw;
         }
         exception = exception2;
         state.InnerBuffer = null;
         if (!(exception2 is WebException) && !(exception2 is SecurityException))
         {
             exception = new WebException(SR.GetString("net_webclient"), exception2);
         }
         AbortRequest(state.Request);
         if ((state != null) && (state.WriteStream != null))
         {
             state.WriteStream.Close();
         }
     }
     finally
     {
         if (flag)
         {
             if (exception == null)
             {
                 state.Close();
             }
             state.CompletionDelegate(state.InnerBuffer, exception, state.AsyncOp);
         }
     }
 }
Пример #3
0
        static private void DownloadBitsReadCallbackState(DownloadBitsState state, IAsyncResult result) {
            Stream stream = state.ReadStream;

            Exception exception = null;
            bool completed = false;

            try {
                int bytesRead = 0;
                if (stream != null && stream != Stream.Null)
                    bytesRead = stream.EndRead(result);
                completed = state.RetrieveBytes(ref bytesRead);
            } catch (Exception e) {
                completed = true;
                if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) {
                    throw;
                }
                exception = e;
                state.InnerBuffer = null;
                if (!(e is WebException || e is SecurityException)) {
                    exception = new WebException(SR.GetString(SR.net_webclient), e);
                }
                AbortRequest(state.Request);
                if(state != null && state.WriteStream != null){
                    state.WriteStream.Close();
                }
            }
            finally {
                if (completed) {
                    if(exception == null){
                        state.Close();
                    }
                    state.CompletionDelegate(state.InnerBuffer, exception, state.AsyncOp);
                }
            }

        }
 private byte[] DownloadBits(WebRequest request, Stream writeStream, CompletionDelegate completionDelegate, AsyncOperation asyncOp)
 {
     WebResponse response = null;
     DownloadBitsState state = new DownloadBitsState(request, writeStream, completionDelegate, asyncOp, this.m_Progress, this);
     if (state.Async)
     {
         request.BeginGetResponse(new AsyncCallback(WebClient.DownloadBitsResponseCallback), state);
         return null;
     }
     response = this.m_WebResponse = this.GetWebResponse(request);
     int bytesRetrieved = state.SetResponse(response);
     while (!state.RetrieveBytes(ref bytesRetrieved))
     {
     }
     state.Close();
     return state.InnerBuffer;
 }