internal virtual Stream DoHTTP(bool isPost) { HttpWebRequest connection; Stream stream; if (isPost) { connection = this.SendPost(); } else { connection = this.SendGet(); } WebResponse response = null; try { MyRequestState state = new MyRequestState { request = connection }; if (connection.BeginGetResponse(new AsyncCallback(HttpProvider.ReadCallback), state) == null) { throw new IOException("Response gathering failed unexpectedly (1)"); } state.allDone.WaitOne(); if (state.ioException != null) { throw state.ioException; } if (state.webException != null) { throw state.webException; } if (state.response == null) { throw new IOException("Response gathering failed unexpectedly (2)"); } response = state.response; stream = response.GetResponseStream(); } catch (Exception e) { if (response != null) { try { response.Close(); } catch (Exception) { } } throw e; } return stream; }
internal virtual Stream DoHTTP(bool isPost) { HttpWebRequest request; Stream responseStream; if (isPost) { request = this.SendPost(); } else { request = this.SendGet(); } WebResponse response = null; try { MyRequestState state = new MyRequestState { request = request }; IAsyncResult result = request.BeginGetResponse(new AsyncCallback(HttpProvider.ReadCallback), state); state.allDone.WaitOne(); if (state.ioException != null) { throw state.ioException; } if (state.webException != null) { throw state.webException; } response = state.response; responseStream = response.GetResponseStream(); } catch (Exception exception) { if (response != null) { try { response.Close(); } catch (Exception) { } } throw exception; } return responseStream; }
protected internal virtual HttpWebRequest SendPost() { streamLogger.Debug("Opening connection to " + this.address); HttpWebRequest connection = CreateWebRequest(this.address, this.cookies); connection.Method = "POST"; connection.ContentType = "application/x-www-form-urlencoded"; MyRequestState state = new MyRequestState { request = connection }; if (connection.BeginGetRequestStream(new AsyncCallback(HttpProvider.WriteCallback), state) == null) { throw new IOException("Request submission failed unexpectedly (1)"); } state.allDone.WaitOne(); if (state.ioException != null) { throw state.ioException; } if (state.webException != null) { throw state.webException; } if (state.stream == null) { throw new IOException("Request submission failed unexpectedly (2)"); } Stream output = state.stream; if (this.request != null) { if (streamLogger.IsDebugEnabled) { streamLogger.Debug("Posting data: " + this.request); } byte[] byte1 = new UTF8Encoding().GetBytes(this.request); output.Write(byte1, 0, byte1.Length); } output.Flush(); output.Close(); return connection; }
protected internal virtual HttpWebRequest SendPost() { streamLogger.Debug("Opening connection to " + this.address); Uri uri = new Uri(this.address); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); if (request == null) { streamLogger.Debug("Failed connection to " + this.address); throw new IOException("Connection failed"); } request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; MyRequestState state = new MyRequestState { request = request }; IAsyncResult result = request.BeginGetRequestStream(new AsyncCallback(HttpProvider.WriteCallback), state); //BEGIN_CI_EDIT if (!state.allDone.WaitOne(LightstreamerDefaults.DEFAULT_TIMEOUT_MS)) { state.ioException = new IOException(string.Format("Timed out after {0}ms waiting for request.BeginGetRequestStream for {1}", LightstreamerDefaults.DEFAULT_TIMEOUT_MS, address)); streamLogger.Warn(state.ioException.Message); } //END_CI_EDIT if (state.ioException != null) { throw state.ioException; } if (state.webException != null) { throw state.webException; } Stream stream = state.stream; if (this.request != null) { if (streamLogger.IsDebugEnabled) { streamLogger.Debug("Posting data: " + this.request); } byte[] bytes = new UTF8Encoding().GetBytes(this.request); stream.Write(bytes, 0, bytes.Length); } stream.Flush(); stream.Close(); return request; }
protected internal virtual HttpWebRequest SendPost() { streamLogger.Debug("Opening connection to " + this.address); Uri requestUri = new Uri(this.address); HttpWebRequest request = (HttpWebRequest) WebRequest.Create(requestUri); if (request == null) { streamLogger.Debug("Failed connection to " + this.address); throw new IOException("Connection failed"); } request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore); request.CachePolicy = policy; MyRequestState state = new MyRequestState { request = request }; IAsyncResult result = request.BeginGetRequestStream(new AsyncCallback(HttpProvider.WriteCallback), state); state.allDone.WaitOne(); if (state.ioException != null) { throw state.ioException; } if (state.webException != null) { throw state.webException; } Stream stream = state.stream; if (this.request != null) { if (streamLogger.IsDebugEnabled) { streamLogger.Debug("Posting data: " + this.request); } byte[] bytes = new UTF8Encoding().GetBytes(this.request); stream.Write(bytes, 0, bytes.Length); } stream.Flush(); stream.Close(); return request; }