Exemplo n.º 1
0
 public void BeginExecuteRequest(
     HttpRequestItem httpRequestItem, 
     List<RequestEnvironmentSetting> requestEnvironmentSettings, 
     Action<Uri, List<RequestEnvironmentSetting>, HttpResponseItem> onSuccess,
     Action<string> onError,
     CancellationToken cancellationToken)
 {
     Task.Factory.StartNew(() => ExecuteRequest(httpRequestItem, requestEnvironmentSettings, onSuccess, onError, cancellationToken, true), cancellationToken);
 }
Exemplo n.º 2
0
        public void RemoveInterceptor(HttpRequestItem httpRequestItemInterceptor)
        {
            interceptors.Remove(httpRequestItemInterceptor);

            if (interceptors.Count == 0)
            {
                Shutdown();
            }
        }
Exemplo n.º 3
0
 public HttpResponseItem(int statusCode, string reasonPhrase, string headers, object body, string description, string contentType, DateTime requestStart, DateTime responseReceived, Double totalRequestSeconds, HttpRequestItem callingRequest)
 {
     StatusCode = statusCode;
     ReasonPhrase = reasonPhrase;
     Headers = headers;
     Body = body;
     Description = description;
     ContentType = contentType;
     RequestStart = requestStart;
     ResponseReceived = responseReceived;
     TotalRequestSeconds = totalRequestSeconds;
     CallingRequest = callingRequest;
 }
Exemplo n.º 4
0
        public void ExecuteRequest(HttpRequestItem httpRequestItem, List<RequestEnvironmentSetting> requestEnvironmentSettings, Action<Uri, List<RequestEnvironmentSetting>, HttpResponseItem> onSuccess,
                            Action<string> onError, CancellationToken cancellationToken, bool callMainThread = false)
        {
            var httpRequestMessage = new HttpRequestMessage();
            using (var client = new HttpClient())
            {
                try
                {
                    httpRequestMessage.RequestUri = new Uri(ReplaceTokensTokens(httpRequestItem.Url, requestEnvironmentSettings));
                    httpRequestMessage.Method = GetHttpMethod(httpRequestItem.Verb);
                    var headerItems =
                        GetHeaderItems(ReplaceTokensTokens(httpRequestItem.Headers,
                                                           requestEnvironmentSettings));
                    SetContentHeadersAndBody(httpRequestMessage, headerItems, httpRequestItem.Verb,
                                             ReplaceTokensTokens(httpRequestItem.Body,
                                                                 requestEnvironmentSettings));
                    SetHeaders(httpRequestMessage, headerItems);

                    DateTime startTime = DateTime.Now;

                    HttpResponseMessage httpResponseMessage;

                    if (cancellationToken == default(CancellationToken))
                    {
                        httpResponseMessage = client.SendAsync(httpRequestMessage).Result;
                    }
                    else
                    {
                        httpResponseMessage = client.SendAsync(httpRequestMessage, cancellationToken).Result;
                    }

                    DateTime completedTime = DateTime.Now;

                    var httpResponseItem = new HttpResponseItem(
                        (int)httpResponseMessage.StatusCode,
                        httpResponseMessage.ReasonPhrase,
                        GetResponseHeaders(httpResponseMessage),
                        SetContent(httpResponseMessage),
                        "",
                        GetContentType(httpResponseMessage),
                        startTime,
                        completedTime,
                        Math.Round((completedTime - startTime).TotalMinutes, 4),
                        httpRequestItem
                        );

                    if (callMainThread)
                    {
                        Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                                   onSuccess, httpRequestMessage.RequestUri,
                                                                   requestEnvironmentSettings, httpResponseItem);
                    }
                    else
                    {
                        onSuccess(httpRequestMessage.RequestUri, requestEnvironmentSettings, httpResponseItem);
                    }
                }
                catch (Exception ex)
                {
                    if (callMainThread)
                    {
                        Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                                   onError,
                                                                   GetBestErrorMessage(ex));
                    }
                    else
                    {
                        onError(GetBestErrorMessage(ex));
                    }
                    return;
                }
            }
        }
Exemplo n.º 5
0
 public void AddInterceptor(HttpRequestItem httpRequestItemInterceptor)
 {
     interceptors.Add(httpRequestItemInterceptor);
 }