Simple HTTP POST in Windows Phone 8

旧街凉风 提交于 2019-12-21 02:58:13

问题


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

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/16138275/simple-http-post-in-windows-phone-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!