在windows store app中将数据发布到服务器

时间:2013-09-16 10:19:19

标签: c# .net windows-8 httpclient

我想以密钥,值对的形式向我的网络服务发送数据。我没有得到如何做到这一点,因为做基本方法的帖子是。

HttpClient httpClient = new HttpClient();
HttpResponseMessage response = httpClient.PostAsync("http://50.16.234.220/helloapp/api/public/", new StringContent("asdad")).Result;

我的Api需要成对才能获取数据,所以请告诉我如何在Windows 8应用程序中执行此操作..非常感谢。

1 个答案:

答案 0 :(得分:2)

试试这个

using System.Net.Http;

protected async override void OnNavigatedTo(NavigationEventArgs e)
{
    var data = new List<KeyValuePair<string, string>>
    {
        new KeyValuePair<string, string>("key_1", "value_1"),
        new KeyValuePair<string, string>("key_2", "value_1")
    };

    await PostKeyValueData(data);
}

private async Task PostKeyValueData(List<KeyValuePair<string, string>> values)
{
    var httpClient = new HttpClient();
    var response = await httpClient.PostAsync("http://50.16.234.220/helloapp/api/public/", new FormUrlEncodedContent(values));
    var responseString = await response.Content.ReadAsStringAsync();
}