Ejemplo n.º 1
0
        static private bool ProcessAsyncResponseStreamResult(WebClientAsyncResult client, IAsyncResult asyncResult)
        {
            bool complete;
            int  bytesRead     = client.ResponseStream.EndRead(asyncResult);
            long contentLength = client.Response.ContentLength;

            if (contentLength > 0 && bytesRead == contentLength)
            {
                // the non-chunked response finished in a single read
                client.ResponseBufferedStream = new MemoryStream(client.Buffer);
                complete = true;
            }
            else if (bytesRead > 0)
            {
                if (client.ResponseBufferedStream == null)
                {
                    int capacity = (int)((contentLength == -1) ? client.Buffer.Length : contentLength);
                    client.ResponseBufferedStream = new MemoryStream(capacity);
                }
                client.ResponseBufferedStream.Write(client.Buffer, 0, bytesRead);
                complete = false;
            }
            else
            {
                complete = true;
            }

            if (complete)
            {
                client.Complete();
            }
            return(complete);
        }
Ejemplo n.º 2
0
        private static bool ProcessAsyncResponseStreamResult(WebClientAsyncResult client, IAsyncResult asyncResult)
        {
            bool flag;
            int  count         = client.ResponseStream.EndRead(asyncResult);
            long contentLength = client.Response.ContentLength;

            if ((contentLength > 0L) && (count == contentLength))
            {
                client.ResponseBufferedStream = new MemoryStream(client.Buffer);
                flag = true;
            }
            else if (count > 0)
            {
                if (client.ResponseBufferedStream == null)
                {
                    int capacity = (contentLength == -1L) ? client.Buffer.Length : ((int)contentLength);
                    client.ResponseBufferedStream = new MemoryStream(capacity);
                }
                client.ResponseBufferedStream.Write(client.Buffer, 0, count);
                flag = false;
            }
            else
            {
                flag = true;
            }
            if (flag)
            {
                client.Complete();
            }
            return(flag);
        }
Ejemplo n.º 3
0
        static private void ProcessAsyncException(WebClientAsyncResult client, Exception e, string method)
        {
            if (Tracing.On)
            {
                Tracing.ExceptionCatch(TraceEventType.Error, typeof(WebClientProtocol), method, e);
            }
            WebException webException = e as WebException;

            if (webException != null && webException.Response != null)
            {
                client.Response = webException.Response;
            }
            else
            {
                // If we've already completed the call then the exception must have come
                // out of the user callback in which case we need to rethrow it here
                // so that it bubbles up to the AppDomain unhandled exception event.
                if (client.IsCompleted)
                {
                    throw new InvalidOperationException(Res.GetString(Res.ThereWasAnErrorDuringAsyncProcessing), e);
                }
                else
                {
                    client.Complete(e);
                }
            }
        }
Ejemplo n.º 4
0
 static private void ReadAsyncResponse(WebClientAsyncResult client)
 {
     if (client.Response.ContentLength == 0)
     {
         client.Complete();
         return;
     }
     try {
         client.ResponseStream = client.Response.GetResponseStream();
         ReadAsyncResponseStream(client);
     }
     catch (Exception e) {
         ProcessAsyncException(client, e);
     }
 }
Ejemplo n.º 5
0
 static private void ReadAsyncResponse(WebClientAsyncResult client)
 {
     if (client.Response.ContentLength == 0)
     {
         client.Complete();
         return;
     }
     try {
         client.ResponseStream = client.Response.GetResponseStream();
         ReadAsyncResponseStream(client);
     }
     catch (Exception e) {
         if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException)
         {
             throw;
         }
         ProcessAsyncException(client, e, "ReadAsyncResponse");
     }
 }
Ejemplo n.º 6
0
        private static void ProcessAsyncException(WebClientAsyncResult client, Exception e, string method)
        {
            if (Tracing.On)
            {
                Tracing.ExceptionCatch(TraceEventType.Error, typeof(WebClientProtocol), method, e);
            }
            WebException exception = e as WebException;

            if ((exception != null) && (exception.Response != null))
            {
                client.Response = exception.Response;
            }
            else
            {
                if (client.IsCompleted)
                {
                    throw new InvalidOperationException(Res.GetString("ThereWasAnErrorDuringAsyncProcessing"), e);
                }
                client.Complete(e);
            }
        }
Ejemplo n.º 7
0
        static private void GetRequestStreamAsyncCallback(IAsyncResult asyncResult)
        {
            WebClientAsyncResult client = (WebClientAsyncResult)asyncResult.AsyncState;

            client.CombineCompletedSynchronously(asyncResult.CompletedSynchronously);
            bool processingRequest = true;

            try {
                Stream requestStream = client.Request.EndGetRequestStream(asyncResult);
                processingRequest = false;
                try {
                    client.ClientProtocol.AsyncBufferedSerialize(client.Request, requestStream, client.InternalAsyncState);
                }
                finally {
                    requestStream.Close();
                }
                client.Request.BeginGetResponse(getResponseAsyncCallback, client);
            }
            catch (Exception e) {
                if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException)
                {
                    throw;
                }
                ProcessAsyncException(client, e, "GetRequestStreamAsyncCallback");
                if (processingRequest)
                {
                    WebException we = e as WebException;
                    if (we != null && we.Response != null)
                    {
                        // ProcessAsyncExcption doesn't call client.Complete() if there's a response,
                        // because it expects us to read the response. However, in certain cases
                        // (e.g. 502 errors), the exception thrown from Request can have a response.
                        // We don't process it, so call Complete() now.
                        client.Complete(e);
                    }
                }
            }
        }
Ejemplo n.º 8
0
        private static void GetRequestStreamAsyncCallback(IAsyncResult asyncResult)
        {
            WebClientAsyncResult asyncState = (WebClientAsyncResult)asyncResult.AsyncState;

            asyncState.CombineCompletedSynchronously(asyncResult.CompletedSynchronously);
            bool flag = true;

            try
            {
                Stream requestStream = asyncState.Request.EndGetRequestStream(asyncResult);
                flag = false;
                try
                {
                    asyncState.ClientProtocol.AsyncBufferedSerialize(asyncState.Request, requestStream, asyncState.InternalAsyncState);
                }
                finally
                {
                    requestStream.Close();
                }
                asyncState.Request.BeginGetResponse(getResponseAsyncCallback, asyncState);
            }
            catch (Exception exception)
            {
                if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException))
                {
                    throw;
                }
                ProcessAsyncException(asyncState, exception, "GetRequestStreamAsyncCallback");
                if (flag)
                {
                    WebException exception2 = exception as WebException;
                    if ((exception2 != null) && (exception2.Response != null))
                    {
                        asyncState.Complete(exception);
                    }
                }
            }
        }
Ejemplo n.º 9
0
 private static void ReadAsyncResponse(WebClientAsyncResult client)
 {
     if (client.Response.ContentLength == 0L)
     {
         client.Complete();
     }
     else
     {
         try
         {
             client.ResponseStream = client.Response.GetResponseStream();
             ReadAsyncResponseStream(client);
         }
         catch (Exception exception)
         {
             if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException))
             {
                 throw;
             }
             ProcessAsyncException(client, exception, "ReadAsyncResponse");
         }
     }
 }
        static private bool ProcessAsyncResponseStreamResult(WebClientAsyncResult client, IAsyncResult asyncResult) {
            bool complete;
            int bytesRead = client.ResponseStream.EndRead(asyncResult);
            long contentLength = client.Response.ContentLength;
            if (contentLength > 0 && bytesRead == contentLength) {
                // the non-chunked response finished in a single read
                client.ResponseBufferedStream = new MemoryStream(client.Buffer);
                complete = true;
            }
            else if (bytesRead > 0) {
                if (client.ResponseBufferedStream == null) {
                    int capacity = (int)((contentLength == -1) ? client.Buffer.Length : contentLength);
                    client.ResponseBufferedStream = new MemoryStream(capacity);
                }
                client.ResponseBufferedStream.Write(client.Buffer, 0, bytesRead);
                complete = false;
            }
            else
                complete = true;

            if (complete)
                client.Complete();
            return complete;
        }
 static private void ReadAsyncResponse(WebClientAsyncResult client) {
     if (client.Response.ContentLength == 0) {
         client.Complete();
         return;
     }
     try {
         client.ResponseStream = client.Response.GetResponseStream();
         ReadAsyncResponseStream(client);
     }
     catch (Exception e) {
         if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) {
             throw;
         }
         ProcessAsyncException(client, e, "ReadAsyncResponse");
     }
 }
 static private void ProcessAsyncException(WebClientAsyncResult client, Exception e, string method) {
     if (Tracing.On) Tracing.ExceptionCatch(TraceEventType.Error, typeof(WebClientProtocol), method, e);
     WebException webException = e as WebException;
     if (webException != null && webException.Response != null) {
         client.Response = webException.Response;
     }
     else {
         // If we've already completed the call then the exception must have come
         // out of the user callback in which case we need to rethrow it here
         // so that it bubbles up to the AppDomain unhandled exception event.
         if (client.IsCompleted)
             throw new InvalidOperationException(Res.GetString(Res.ThereWasAnErrorDuringAsyncProcessing), e);
         else
             client.Complete(e);
     }
 }
 private static void ReadAsyncResponse(WebClientAsyncResult client)
 {
     if (client.Response.ContentLength == 0L)
     {
         client.Complete();
     }
     else
     {
         try
         {
             client.ResponseStream = client.Response.GetResponseStream();
             ReadAsyncResponseStream(client);
         }
         catch (Exception exception)
         {
             if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException))
             {
                 throw;
             }
             ProcessAsyncException(client, exception, "ReadAsyncResponse");
         }
     }
 }
 private static bool ProcessAsyncResponseStreamResult(WebClientAsyncResult client, IAsyncResult asyncResult)
 {
     bool flag;
     int count = client.ResponseStream.EndRead(asyncResult);
     long contentLength = client.Response.ContentLength;
     if ((contentLength > 0L) && (count == contentLength))
     {
         client.ResponseBufferedStream = new MemoryStream(client.Buffer);
         flag = true;
     }
     else if (count > 0)
     {
         if (client.ResponseBufferedStream == null)
         {
             int capacity = (contentLength == -1L) ? client.Buffer.Length : ((int) contentLength);
             client.ResponseBufferedStream = new MemoryStream(capacity);
         }
         client.ResponseBufferedStream.Write(client.Buffer, 0, count);
         flag = false;
     }
     else
     {
         flag = true;
     }
     if (flag)
     {
         client.Complete();
     }
     return flag;
 }
 private static void ProcessAsyncException(WebClientAsyncResult client, Exception e, string method)
 {
     if (Tracing.On)
     {
         Tracing.ExceptionCatch(TraceEventType.Error, typeof(WebClientProtocol), method, e);
     }
     WebException exception = e as WebException;
     if ((exception != null) && (exception.Response != null))
     {
         client.Response = exception.Response;
     }
     else
     {
         if (client.IsCompleted)
         {
             throw new InvalidOperationException(Res.GetString("ThereWasAnErrorDuringAsyncProcessing"), e);
         }
         client.Complete(e);
     }
 }