Example #1
0
        private void SendWebRequest <TResponse>(string httpMethod, string absoluteUrl, object request, CancellationToken token,
                                                Action <TResponse> onSuccess, Action <object, Exception> onError, Action <WebResponse> onResponseInit = null)
        {
            if (httpMethod == null)
            {
                throw new ArgumentNullException(nameof(httpMethod));
            }

            this.PopulateRequestMetadata(request);

            var requestUri     = absoluteUrl;
            var hasQueryString = request != null && !httpMethod.HasRequestBody();

            if (hasQueryString)
            {
                var queryString = QueryStringSerializer.SerializeToString(request);
                if (!string.IsNullOrEmpty(queryString))
                {
                    requestUri += "?" + queryString;
                }
            }

            var webRequest = this.CreateHttpWebRequest(requestUri);

            var requestState = new AsyncState <TResponse>(BufferSize)
            {
                HttpMethod                = httpMethod,
                Url                       = requestUri,
                WebRequest                = webRequest,
                Request                   = request,
                Token                     = token,
                OnResponseInit            = onResponseInit,
                OnSuccess                 = onSuccess,
                OnError                   = onError,
                UseSynchronizationContext = CaptureSynchronizationContext ? SynchronizationContext.Current : null,
                HandleCallbackOnUIThread  = HandleCallbackOnUiThread,
            };

            if (!DisableTimer)
            {
                requestState.StartTimer(this.Timeout.GetValueOrDefault(DefaultTimeout));
            }

            SendWebRequestAsync(httpMethod, request, requestState, webRequest);
        }
        private void SendWebRequest <TResponse>(string httpMethod, string absoluteUrl, object request,
                                                Action <TResponse> onSuccess, Action <TResponse, Exception> onError)
        {
            if (httpMethod == null)
            {
                throw new ArgumentNullException("httpMethod");
            }

            var requestUri            = absoluteUrl;
            var httpGetOrDeleteOrHead = (httpMethod == "GET" || httpMethod == "DELETE" || httpMethod == "HEAD");
            var hasQueryString        = request != null && httpGetOrDeleteOrHead;

            if (hasQueryString)
            {
                var queryString = QueryStringSerializer.SerializeToString(request);
                if (!string.IsNullOrEmpty(queryString))
                {
                    requestUri += "?" + queryString;
                }
            }

            var webRequest = this.CreateHttpWebRequest(requestUri);

            var requestState = new AsyncState <TResponse>(BufferSize)
            {
                HttpMethod = httpMethod,
                Url        = requestUri,
                WebRequest = webRequest,
                Request    = request,
                OnSuccess  = onSuccess,
                OnError    = onError,
                UseSynchronizationContext = CaptureSynchronizationContext ? SynchronizationContext.Current : null,
                HandleCallbackOnUIThread  = HandleCallbackOnUiThread,
            };

            requestState.StartTimer(this.Timeout.GetValueOrDefault(DefaultTimeout));

            SendWebRequestAsync(httpMethod, request, requestState, webRequest);
        }