void GetResponse(MemoryStream responseStream, out HttpPacket header, int readTimeout) { int bytes = 0; byte[] responseBuffer = new byte[2048]; bool bContinue = false; // read response do { DateTime startTime = DateTime.Now; IAsyncResult result = _networkStream.BeginRead(responseBuffer, 0, responseBuffer.Length); // wait for bytes received, stop event or timeout WaitHandle[] handles; if (_executionController != null && _executionController.StopEvent != null) { handles = new WaitHandle[] { result.AsyncWaitHandle, _executionController.StopEvent }; } else { handles = new WaitHandle[] { result.AsyncWaitHandle }; } int handle = System.Threading.WaitHandle.WaitAny(handles, readTimeout); if (handle == WaitHandle.WaitTimeout) { _networkStream.Close(); throw new IOException("The HTTP request has exceeded the allotted timeout"); } if (handle == 1) { //System.Diagnostics.Debug.WriteLine("Stop event"); _networkStream.Close(); _executionController.ReportStop(); } bytes = _networkStream.EndRead(result); DateTime endTime = DateTime.Now; //System.Diagnostics.Debug.WriteLine(string.Format("--- Bytes received: {0}", bytes)); responseStream.Write(responseBuffer, 0, bytes); // timeout for next part of answer readTimeout -= (int)((endTime - startTime).TotalMilliseconds); // parse response received by this moment. try { bContinue = HttpHelper.ContinueReading(responseStream, out header); } catch (Exception exc) { _networkStream.Close(); // clean resources somehow ? throw new Exception("An error occurred while parsing HTTP packet", exc); } if (readTimeout < 0) { _networkStream.Close(); throw new IOException("The HTTP request has exceeded the allotted timeout"); } System.Diagnostics.Debug.WriteLine(string.Format("DataAvailable: {0}, continue: {1} ", _networkStream.DataAvailable, bContinue)); }while (_networkStream.DataAvailable || bContinue); }
public string SendSoapMessage(string request) { // create request bytes byte[] soapBytes = Encoding.UTF8.GetBytes(request); // create headers byte[] httpHeaders = HttpHelper.CreateHttpHeaders(soapBytes.Length, _address.AbsolutePath, _address.Host); //whole message with headers byte[] bytes = new byte[httpHeaders.Length + soapBytes.Length]; Array.Copy(httpHeaders, bytes, httpHeaders.Length); Array.Copy(soapBytes, 0, bytes, httpHeaders.Length, soapBytes.Length); #if DEBUG //string str = Encoding.UTF8.GetString(bytes); //System.Diagnostics.Debug.WriteLine(string.Format("SEND: {0}", str)); #endif RequestNetworkStream _networkStream = new RequestNetworkStream(new EndpointAddress(_address.OriginalString)); _networkStream.Connect(); _networkStream.Write(bytes, 0, bytes.Length); MemoryStream responseStream = new MemoryStream(); byte[] responseBuffer = new byte[2048]; int readTimeout = _timeout; int bytesCount = 0; bool bContinue = false; HttpPacket header = null; do { DateTime startTime = DateTime.Now; IAsyncResult result = _networkStream.BeginRead(responseBuffer, 0, responseBuffer.Length); // wait for bytes receied, stop event or timeout WaitHandle[] handles; if (_executionController != null && _executionController.StopEvent != null) { handles = new WaitHandle[] { result.AsyncWaitHandle, _executionController.StopEvent }; } else { handles = new WaitHandle[] { result.AsyncWaitHandle }; } int handle = System.Threading.WaitHandle.WaitAny(handles, readTimeout); if (handle == WaitHandle.WaitTimeout) { _networkStream.Close(); throw new IOException("The HTTP request has exceeded the allotted timeout"); } if (handle == 1) { System.Diagnostics.Debug.WriteLine("Stop event"); _networkStream.Close(); _executionController.ReportStop(); } bytesCount = _networkStream.EndRead(result); DateTime endTime = DateTime.Now; System.Diagnostics.Debug.WriteLine(string.Format("--- Bytes received: {0}", bytesCount)); responseStream.Write(responseBuffer, 0, bytesCount); // timeout for next part of answer readTimeout -= (int)((endTime - startTime).TotalMilliseconds); // parse response received by this moment. try { bContinue = HttpHelper.ContinueReading(responseStream, out header); } catch (Exception exc) { _networkStream.Close(); // clean resources somehow ? throw new Exception("An error occurred while parsing HTTP packet", exc); } if (readTimeout < 0) { _networkStream.Close(); throw new IOException("The HTTP request has exceeded the allotted timeout"); } }while (_networkStream.DataAvailable || bContinue); _networkStream.Close(); int count = (int)responseStream.Length - header.BodyOffset; if (header.ContentLength < count) { throw new HttpProtocolException( string.Format("An error occurred while receiving packet. Expected length: {0}, received: {1}", header.ContentLength, count)); } // parse response and notify listeners string response = HttpHelper.GetFormattedMessage(responseStream.GetBuffer(), header.BodyOffset); responseStream.Close(); return(response); }