protected abstract void RequestSent(HttpRequestState request);
protected abstract void RequestReply(HttpRequestState state, bool success, WebException exception);
protected void ReadCallback(IAsyncResult result) { try { _RequestState = (HttpRequestState)result.AsyncState; Stream responseStream = _RequestState.ResponseStream; int read = responseStream.EndRead(result); // Check if we have read the entire response if (read > 0) { // Create the byte array if it hasn't been created yet if (_RequestState.ResponseData == null || _RequestState.ResponseData.Length != _RequestState.WebResponse.ContentLength) _RequestState.ResponseData = new byte[_RequestState.WebResponse.ContentLength]; // Copy the current buffer data in to the response variable Buffer.BlockCopy(_RequestState.BufferRead, 0, _RequestState.ResponseData, _RequestState.ResponseDataPos, read); // Increment our writing position in the response variable _RequestState.ResponseDataPos += read; // Continue reading the response until EndRead() returns 0 IAsyncResult asynchronousResult = responseStream.BeginRead(_RequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallback), _RequestState); return; } else { // Fire the callback for receiving a response try { RequestReply(_RequestState, true, null); } catch (Exception e) { SecondLife.LogStatic(e.ToString(), Helpers.LogLevel.Error); } responseStream.Close(); } } catch (WebException e) { Stop(false, e); } }
private void ResponseCallback(IAsyncResult result) { try { _RequestState = (HttpRequestState)result.AsyncState; _RequestState.WebResponse = (HttpWebResponse)_RequestState.WebRequest.EndGetResponse(result); // Read the response into a Stream object Stream responseStream = _RequestState.WebResponse.GetResponseStream(); _RequestState.ResponseStream = responseStream; // Begin reading of the contents of the response IAsyncResult asynchronousInputRead = responseStream.BeginRead(_RequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallback), _RequestState); // If there is a timeout, the callback fires and the request becomes aborted ThreadPool.RegisterWaitForSingleObject(asynchronousInputRead.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _RequestState, HTTP_TIMEOUT, true); } catch (WebException e) { Stop(false, e); } }
protected void RequestStreamCallback(IAsyncResult result) { try { _RequestState = (HttpRequestState)result.AsyncState; Stream reqStream = _RequestState.WebRequest.EndGetRequestStream(result); reqStream.Write(_RequestState.RequestData, 0, _RequestState.RequestData.Length); reqStream.Close(); IAsyncResult newResult = _RequestState.WebRequest.BeginGetResponse(new AsyncCallback(ResponseCallback), _RequestState); // If there is a timeout, the callback fires and the request becomes aborted ThreadPool.RegisterWaitForSingleObject(newResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _RequestState, HTTP_TIMEOUT, true); } catch (WebException e) { Stop(false, e); } }
public void Start() { if (_Listener != null) { // Server mode _Listener.Start(); _Listener.BeginGetContext(_ServerCallback, _Listener); } else if (!String.IsNullOrEmpty(_RequestURL)) { // Client mode HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(_RequestURL); IAsyncResult result; // Always disable keep-alive for our purposes httpRequest.KeepAlive = false; // Create a state object to track this request in async callbacks _RequestState = new HttpRequestState(httpRequest); _RequestState.State = _State; if (!String.IsNullOrEmpty(_ProxyURL)) { // Create a proxy object WebProxy proxy = new WebProxy(); // Associate a new Uri object to the _wProxy object, using the proxy address // selected by the user proxy.Address = new Uri(_ProxyURL); // Finally, initialize the Web request object proxy property with the _wProxy // object httpRequest.Proxy = proxy; } try { if (_PostData != null) { // POST request _RequestState.WebRequest.Method = "POST"; _RequestState.WebRequest.ContentLength = _PostData.Length; if (!String.IsNullOrEmpty(_ContentType)) _RequestState.WebRequest.ContentType = _ContentType; _RequestState.RequestData = _PostData; result = (IAsyncResult)_RequestState.WebRequest.BeginGetRequestStream( new AsyncCallback(RequestStreamCallback), _RequestState); } else { // GET request result = (IAsyncResult)_RequestState.WebRequest.BeginGetResponse( new AsyncCallback(ResponseCallback), _RequestState); } // If there is a timeout, the callback fires and the request becomes aborted ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _RequestState, HTTP_TIMEOUT, true); } catch (WebException e) { Stop(false, e); return; } // If we get here the request has been initialized, so fire the callback for a request being started RequestSent(_RequestState); } else { SecondLife.LogStatic("HttpBase.Start() called with no client or server mode initialized", Helpers.LogLevel.Error); } }