Click here to Skip to main content
15,881,248 members
Articles / Mobile Apps / Windows Phone 7

An HTTP POST Client for Windows Phone 7

Rate me:
Please Sign up or sign in to vote.
4.25/5 (3 votes)
19 May 2011CPOL1 min read 22.5K   4   2
An easy-to-use, thread-safe library which works similarly to WebClient!

Silverlight and Silverlight for Windows Phone offer a great way of simplifying HTTP GET requests: WebClient class. WebClient encapsulates the primary logic of the HttpWebRequest and HttpWebResponse classes in order to receive GET data. These classes are completely transparent in most common scenarios. All the hard work is done by a WebClient object:

C#
WebClient proxy = new WebClient();
proxy.DownloadStringCompleted += (sender, e) =>
{
    if (e.Error == null)
    {
        //Process the result...
        string data = e.Result;
    }
};
proxy.DownloadStringAsync(new Uri("http://address.com/service", UriKind.Absolute));

But how about HTTP POST requests? POST requests are critical because most mobile applications communicate via Web Services. Until today, we had to handle the HttpWebRequest and HttpWebResponse classes ourselves in order to acquire the desired data.

Not any more!

PostClient

I have just released PostClient, an easy-to-use, thread-safe library which works similarly to WebClient! Have a look:

C#
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("name", "Vangos");
parameters.Add("age", 23);

PostClient proxy = new PostClient(parameters);
proxy.DownloadStringCompleted += (sender, e) =>
{
    if (e.Error == null)
    {
        //Process the result...
        string data = e.Result;
    }
};
proxy.DownloadStringAsync(new Uri("http://address.com/service", UriKind.Absolute));

Quite simple, huh? No HttpWebRequests, no HttpWebResponses! You only need to create a Dictionary of key-value pairs and pass it as a parameter to the PostClient's constructor. PostClient will then asynchronously download a string containing the web service data.

PostClient is an Open-Source library hosted in CodePlex and licensed under Microsoft Public License (MS-PL). You are free to use it in your applications by simply mentioning its origin. If you want to contribute for further development, do not hesitate to contact me.

Credits

Credits to Vish Uma for his great blog post considering asynchronous POST requests in Silverlight.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionC# to VB Pin
Rashid Aliyev11-Mar-13 11:54
Rashid Aliyev11-Mar-13 11:54 
GeneralLicense Info Pin
Dan Colasanti15-Jun-11 20:37
professionalDan Colasanti15-Jun-11 20:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.