Пример #1
1
 public EventSignal<IResponse> PostAsync(string url, Action<IRequest> prepareRequest, Dictionary<string, string> postData)
 {
     var returnSignal = new EventSignal<IResponse>();
     var signal = HttpHelper.PostAsync(url, request => prepareRequest(new HttpWebRequestWrapper(request)), postData);
     signal.Finished += (sender, e) => returnSignal.OnFinish(new HttpWebResponseWrapper(e.Result.Result) { Exception = e.Result.Exception, IsFaulted = e.Result.IsFaulted });
     return returnSignal;
 }
        public EventSignal<IResponse> GetAsync(string url, Action<IRequest> prepareRequest)
        {
            var _returnSignal = new EventSignal<IResponse>();
            var _signal = HttpHelper.GetAsync(url, request => prepareRequest(new HttpWebRequestWrapper(request)));

            _signal.Finished += (sender, e) => _returnSignal.OnFinish(new HttpWebResponseWrapper(e.Result.Result)
            {
                Exception = e.Result.Exception,
                IsFaulted = e.Result.IsFaulted
            });

            return _returnSignal;
        }
Пример #3
0
        public EventSignal <IResponse> GetAsync(string url, Action <IRequest> prepareRequest)
        {
            var _returnSignal = new EventSignal <IResponse>();
            var _signal       = HttpHelper.GetAsync(url, request => prepareRequest(new HttpWebRequestWrapper(request)));

            _signal.Finished += (sender, e) => _returnSignal.OnFinish(new HttpWebResponseWrapper(e.Result.Result)
            {
                Exception = e.Result.Exception,
                IsFaulted = e.Result.IsFaulted
            });

            return(_returnSignal);
        }
Пример #4
0
        private static EventSignal <CallbackDetail <HttpWebResponse> > PostInternal(
            string url,
            Action <HttpWebRequest> requestPreparer,
            IDictionary <string, string> postData)
        {
            var _request = (HttpWebRequest)HttpWebRequest.Create(url);

            if (requestPreparer != null)
            {
                requestPreparer(_request);
            }

            var buffer = ProcessPostData(postData);

            _request.Method      = "POST";
            _request.ContentType = "application/x-www-form-urlencoded";
            // Set the content length if the buffer is non-null
            _request.ContentLength = buffer != null ? buffer.LongLength : 0;

            EventSignal <CallbackDetail <HttpWebResponse> > _signal =
                new EventSignal <CallbackDetail <HttpWebResponse> >();

            if (buffer == null)
            {
                // If there's nothing to be written to the request then just get the response
                GetResponseAsync(_request, _signal);
                return(_signal);
            }

            RequestState _requestState = new RequestState
            {
                PostData = buffer,
                Request  = _request,
                Response = _signal
            };

            try
            {
                _request.BeginGetRequestStream(GetRequestStreamCallback, _requestState);
            }
            catch (Exception ex)
            {
                _signal.OnFinish(new CallbackDetail <HttpWebResponse> {
                    IsFaulted = true, Exception = ex
                });
            }
            return(_signal);
        }
Пример #5
0
 public static void GetResponseAsync(HttpWebRequest request, EventSignal<CallbackDetail<HttpWebResponse>> signal)
 {
     try
     {
         request.BeginGetResponse(
             GetResponseCallback,
             new RequestState
             {
                 Request = request,
                 PostData = new byte[] { },
                 Response = signal
             });
     }
     catch (Exception ex)
     {
         signal.OnFinish(new CallbackDetail<HttpWebResponse> { Exception = ex, IsFaulted = true });
     }
 }
Пример #6
0
 public static void GetResponseAsync(HttpWebRequest request, EventSignal <CallbackDetail <HttpWebResponse> > signal)
 {
     try
     {
         request.BeginGetResponse(
             GetResponseCallback,
             new RequestState
         {
             Request  = request,
             PostData = new byte[] { },
             Response = signal
         });
     }
     catch (Exception ex)
     {
         signal.OnFinish(new CallbackDetail <HttpWebResponse> {
             Exception = ex, IsFaulted = true
         });
     }
 }
Пример #7
0
        private static EventSignal<CallbackDetail<HttpWebResponse>> PostInternal(
            string url,
            Action<HttpWebRequest> requestPreparer,
            IDictionary<string, string> postData)
        {
            var _request = (HttpWebRequest)HttpWebRequest.Create(url);

            if (requestPreparer != null)
                requestPreparer(_request);

            byte[] _buffer = ProcessPostData(postData);

            _request.Method = "POST";
            _request.ContentType = "application/x-www-form-urlencoded";
            // Set the content length if the buffer is non-null
            _request.ContentLength = _buffer != null ? _buffer.LongLength : 0;

            EventSignal<CallbackDetail<HttpWebResponse>> _signal =
                new EventSignal<CallbackDetail<HttpWebResponse>>();

            if (_buffer == null)
            {
                // If there's nothing to be written to the request then just get the response
                GetResponseAsync(_request, _signal);
                return _signal;
            }

            RequestState _requestState = new RequestState
            {
                PostData = _buffer,
                Request = _request,
                Response = _signal
            };

            try
            {
                _request.BeginGetRequestStream(GetRequestStreamCallback, _requestState);
            }
            catch (Exception ex)
            {
                _signal.OnFinish(new CallbackDetail<HttpWebResponse> { IsFaulted = true, Exception = ex });
            }
            return _signal;
        }