예제 #1
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;
                }
            }
        }
 private void HandleResponse(Uri arg1, List<RequestEnvironmentSetting> arg2, HttpResponseItem httpResponseItem)
 {
     HttpRequestSequenceViewModel viewModel;
     HttpRequestSequenceViewModel.RunningWorkflows.TryGetValue(workflowInstanceId, out viewModel);
     httpResponseItem.Description = Description;
     AddOnUi(viewModel.Responses, httpResponseItem);
 }
 private void Includes(NativeActivityContext context, HttpResponseItem lastResponse)
 {
     switch (SelectedResponseSectionIndex)
     {
         case 0:
             Evaluate(context, lastResponse.StatusCode.ToString(CultureInfo.InvariantCulture).Contains(ConditionValue));
             break;
         case 1:
             Evaluate(context, lastResponse.Headers.Contains(ConditionValue));
             break;
         case 2:
             Evaluate(context, lastResponse.Body.ToString().Contains(ConditionValue));
             break;
     }
 }
 private static void AddOnUi(ObservableCollection<HttpResponseItem> responses, HttpResponseItem item)
 {
     Action<HttpResponseItem> addMethod = responses.Add;
     Application.Current.Dispatcher.BeginInvoke(addMethod, item);
 }
 private void Equals(NativeActivityContext context, HttpResponseItem lastResponse)
 {
     switch (SelectedResponseSectionIndex)
     {
         case 0:
             Evaluate(context, lastResponse.StatusCode == int.Parse(ConditionValue));
             break;
         case 1:
             Evaluate(context, lastResponse.Headers == ConditionValue);
             break;
         case 2:
             Evaluate(context, lastResponse.Body.ToString() == ConditionValue);
             break;
     }
 }