Skip to content

Useful Tip for making an Async Post to URL

December 9, 2015

This is something I found which is rather useful using C# is to make a HTTP post to a specific url with additional parameters in the code :-

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;

namespace Demo
{
    public class Example
    {
        public async void AsyncPost()
        {
            var values = new Dictionary<string, string>();
            values.Add("ThisIs", "Annoying");
            var content = new FormUrlEncodedContent(values);

            using (var client = new HttpClient())
            {
                try
                {
                    var httpResponseMessage = await client.PostAsync("http://SomeUrl.somewhere", content);

                    if (httpResponseMessage.StatusCode == HttpStatusCode.OK)
                    {
                        // Do something...
                    }
                }
                catch (OperationCanceledException) { }
            }
        }
    }
}
Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: