Exemplo n.º 1
0
        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;
        }
Exemplo n.º 2
0
        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);
        }