예제 #1
0
        /// <summary>
        ///     Find offer with specified Id, serialize it to JSON and POST request JSON to specified URI
        /// </summary>
        /// <param name="model"></param>
        /// <param name="id">Offer Id</param>
        /// <param name="uri">URI to POST request</param>
        /// <returns></returns>
        public static async Task <string> Post(yml_catalog model, string id, Uri uri)
        {
            //Imitation of long-going operation
            await Task.Run(() => Thread.Sleep(5000));

            await Task.Run(() =>
            {
                offer offer = model.shop.offers.First(x => x.id == id);

                Json = JsonConvert.SerializeObject(offer,
                                                   Formatting.Indented,
                                                   new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                });
            });

            string response;

            using (var client = new HttpClient())
            {
                HttpContent content = new StringContent(Json);
                content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
                HttpResponseMessage responseMessage = await client.PostAsync(uri, content);

                response = await responseMessage.Content.ReadAsStringAsync();
            }

            return(response);
        }
예제 #2
0
        private async void GetButtonClick(object sender, RoutedEventArgs e)
        {
            //Update UI
            getButton.IsEnabled         = false;
            progressBar.IsIndeterminate = true;
            statusTestBlock.Text        = "Getting and deserializing...";
            responseLink.Visibility     = Visibility.Hidden;

            //Sending GET request and deserializing response to model
            try
            {
                _model = await Utils.Get(Url);
            }
            catch (Exception ex)
            {
                getButton.IsEnabled         = true;
                progressBar.IsIndeterminate = false;
                statusTestBlock.Text        = "Error.";
                MessageBox.Show(String.Format("Failed to GET.{0}\n Exception message: {1}\n StackTrace: {2}",
                                              ex.GetType(), ex.Message, ex.StackTrace));
                return;
            }

            //Update UI
            postButton.IsEnabled        = true;
            progressBar.IsIndeterminate = false;
            statusTestBlock.Text        = "Done!";
        }
예제 #3
0
        private async void SendButtonClick(object sender, RoutedEventArgs e)
        {
            Uri uri = null;

            //Trying to parse string to Uri
            if (String.IsNullOrEmpty(PostTo))
            {
                MessageBox.Show("Please specify URL.");
                return;
            }
            Uri.TryCreate(PostTo, UriKind.Absolute, out uri);
            if (uri == null)
            {
                Uri.TryCreate("http://" + PostTo, UriKind.Absolute, out uri);
            }

            if (uri == null)
            {
                MessageBox.Show("Please specify valid URL.");
                return;
            }

            //Update UI
            statusTestBlock.Text        = "Sending POST request...";
            progressBar.IsIndeterminate = true;

            //Sending POST request in background
            Response = await Utils.Post(_model, Id, uri);

            //Update UI
            statusTestBlock.Text        = "Done!";
            responseLink.Visibility     = Visibility.Visible;
            progressBar.IsIndeterminate = false;
            getButton.IsEnabled         = true;
            postButton.IsEnabled        = false;
            //Reset model
            _model = null;
        }