mjhannaf
mjhannaf

Reputation: 53

Simple HTTP POST in Windows Phone 8

I have a string that I need to POST in Windows Phone 8. It looks like this:

https://www.scoreoid.com/api/getPlayers?api_key=[apiKey]&game_id=[gameID]&response=xml&username=[username]&password=[password]

This string simply returns another string (that is formatted as XML that I parse later in my code).

I have yet to find a simple solution to this like in Windows 8.

Edit: Found the solution to my problem with an assist from rciovati and the HttpClient library.

Here's my simple code:

        var httpClient = new HttpClient();
        return await httpClient.GetStringAsync(uri + "?" + post_data);

Upvotes: 2

Views: 12737

Answers (2)

rciovati
rciovati

Reputation: 28073

Using the new Http Client Library is quite easy:

var values = new List<KeyValuePair<string, string>>
                    {
                        new KeyValuePair<string, string>("api_key", "12345"),
                        new KeyValuePair<string, string>("game_id", "123456")
                    };

var httpClient = new HttpClient(new HttpClientHandler());
HttpResponseMessage response = await httpClient.PostAsync(url, new FormUrlEncodedContent(values));
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();

You can find other informations about this library here.

Upvotes: 24

Tarek Wahab
Tarek Wahab

Reputation: 104

Here's a pretty useful blog post from Andy Wigley about how to do Http networking on Windows Phone 8. The WinPhoneExtensions wrapper library he speaks of basically simulates the async/await model of network programming you can do in Win8.

http://blogs.msdn.com/b/andy_wigley/archive/2013/02/07/async-and-await-for-http-networking-on-windows-phone.aspx

Upvotes: 0

Related Questions