Пример #1
0
 /// <summary>
 /// Inicia o processo assincrono de recuperação da stream de requisição.
 /// </summary>
 /// <param name="request"></param>
 /// <param name="callback"></param>
 /// <param name="state"></param>
 /// <returns></returns>
 public static IAsyncResult BeginGetRequestStream(System.Net.HttpWebRequest request, AsyncCallback callback, object state)
 {
     try
     {
         if (request.IsAborted())
         {
             throw new System.Net.WebException("The request was canceled.", System.Net.WebExceptionStatus.RequestCanceled);
         }
         bool flag = (((request.Method != "GET") && (request.Method != "CONNECT")) && (request.Method != "HEAD")) && (request.Method != "TRACE");
         if (request.Method == null || !flag)
         {
             throw new System.Net.ProtocolViolationException("Cannot send data when method is: " + request.Method);
         }
         if ((request.ContentLength == -1L && !request.SendChunked) && (request.KeepAlive))
         {
             throw new System.Net.ProtocolViolationException("Content-Length not set");
         }
         string transferEncoding = request.TransferEncoding;
         if ((!request.SendChunked && (transferEncoding != null)) && (transferEncoding.Trim() != string.Empty))
         {
             throw new System.Net.ProtocolViolationException("SendChunked should be true.");
         }
         object locker = request.GetLock();
         lock (locker)
         {
             if (request.IsResponseCalled())
             {
                 throw new InvalidOperationException("The operation cannot be performed once the request has been submitted.");
             }
             if (request.GetAsyncWrite() != null)
             {
                 throw new InvalidOperationException("Cannot re-call start of asynchronous method while a previous call is still in progress.");
             }
             request.SetAsyncWrite(CreateWebAsyncResult(request, callback, state));
             request.SetInitialMethod(request.Method);
             var          writeStream = request.GetWriteStream();
             IAsyncResult asyncWrite  = null;
             if (request.HaveRequest() && (writeStream != null))
             {
                 asyncWrite = request.GetAsyncWrite();
                 asyncWrite.SetCompleted(true, writeStream);
                 asyncWrite.DoCallback();
                 return(request.GetAsyncWrite());
             }
             request.SetGotRequestStream(true);
             asyncWrite = request.GetAsyncWrite();
             if (!request.IsRequestSent())
             {
                 request.SetRequestSent(true);
                 request.SetRedirects(0);
                 var servicePoint = request.GetServicePoint();
                 request.SetServicePoint(servicePoint);
                 request.SetAbortHandler(servicePoint.SendRequest(request, request.GetConnectionGroup()));
             }
             return(asyncWrite);
         }
     }
     catch (Exception ex)
     {
         throw new Exception(ex.ToString(), ex);
     }
 }