Simple HTTP POST in Windows Phone 8

12,717

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.

Share:
12,717
mjhannaf
Author by

mjhannaf

Updated on June 04, 2022

Comments

  • mjhannaf
    mjhannaf almost 2 years

    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);