如何在Windows应用商店中通过HTTP POST方法发布XML数据?

时间:2013-08-26 09:48:53

标签: windows-8 windows-runtime microsoft-metro windows-store-apps dotnet-httpclient

在Windows 8应用程序中使用休息服务的任何示例方法。请告诉我可能的链接。

2 个答案:

答案 0 :(得分:4)

试试这个

using System.IO;
using System.Net.Http;
using System.Text;

var objHttpClient = new HttpClient();
var formParameters = "<details><id>1</id><name>test</name></details>");
var objHttpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://example.com/page");
objHttpRequestMessage.Content = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes(formParameters)));
objHttpRequestMessage.Content.Headers.Add("Content-Type", "text/xml");
var result = await objHttpClient.SendAsync(objHttpRequestMessage);

同时检查HttpClient sample on MSDN

答案 1 :(得分:2)

使用System.Net.Http.StringContent课怎么样?

using System.Net.Http;
using System.Text;

private async void Button_Click_1(object sender, RoutedEventArgs e)
{
    var contentString = "<details><id>1</id><name>test</name></details>";
    var httpClient = new HttpClient();
    var httpRequestMessage = new HttpRequestMessage(
        HttpMethod.Post,
        "http://example.com/page");
    httpRequestMessage.Content = new StringContent(
        contentString,
        Encoding.UTF8,
        "text/xml");
    var result = await httpClient.SendAsync(httpRequestMessage);
}