public IAsyncResult Execute(string url, ExecuteListener listener) { WebRequest request = null; try { // Create a new webrequest to the mentioned URL. request = WebRequest.Create(url); } catch (Exception e) { log.Error(string.Format("Create WebRequest for url [{0}] failed", url), e); listener.ExecuteCompleted(null, e); return null; } //Callback parameter RequestStateInfo requestStateInfo = new RequestStateInfo(); requestStateInfo.url = url; requestStateInfo.request = request; requestStateInfo.listener = listener; IAsyncResult asyncResult = null; try { // Start the Asynchronous call for response. asyncResult = (IAsyncResult)request.BeginGetResponse(new AsyncCallback(RespCallback), requestStateInfo); } catch (Exception e) { requestStateInfo.errorMessage = string.Format("Execute for BeginGetResponse [{0}] failed", url); ExecuteDone(requestStateInfo, e); } return asyncResult; }
private void ExecuteDone(RequestStateInfo requestStateInfo, Exception e) { //log the exception if there is one if(e != null) { log.Error(requestStateInfo.errorMessage, e); } //close the stream and release the connection if(requestStateInfo.response != null) { CloseQuietly(requestStateInfo.response); } //execute the listener requestStateInfo.listener.ExecuteCompleted(requestStateInfo, e); }