简体   繁体   English

使用Windows.Web.HttpClient PostAsync()将带有字符串参数的POST请求发送到URL

[英]Send POST request with string parameters to an URL using Windows.Web.HttpClient PostAsync()

I am able to send a POST request with string parameters to an URL using the System.Web.HttpClient like so: 我可以使用System.Web.HttpClient将带有字符串参数的POST请求发送到URL,如下所示:

// Create the HTTPClient
HttpClient httpClient = new HttpClient();

// Add string parameters
FormUrlEncodedContent content = new FormUrlEncodedContent(new[] {
    new KeyValuePair<string, string>("client_id", "myclientid),
    new KeyValuePair<string, string>("serial_number", "myserialnumber)
});

// Make the call
HttpResponseMessage response = await httpClient.PostAsync(_requestUri, content);

However, I want to do the same, but with the Windows.Web.HttpClient class. 但是,我想使用Windows.Web.HttpClient类执行相同的操作。
The main difference is that the PostAsync method accepts an HttpContent as the second argument, so my FormUrlEncodedContent does not work. 主要区别在于PostAsync方法接受HttpContent作为第二个参数,因此我的FormUrlEncodedContent不起作用。 Also I cannot create an IHttpContent with JSON since I need to pass string parameters. 另外,由于需要传递字符串参数,因此无法使用JSON创建IHttpContent

Any thoughts? 有什么想法吗?

Take a look at 看一眼

http://msdn.microsoft.com/en-us/library/windows/apps/windows.web.http.httpformurlencodedcontent http://msdn.microsoft.com/en-us/library/windows/apps/windows.web.http.httpformurlencodedcontent

I reckon all you need is to create a HttpFormUrlEncodedContent object instead and pass that in. It implements the IHTTPContext interface which is what your after 我认为您所需要的只是创建一个HttpFormUrlEncodedContent对象并将其传递给它。它实现了IHTTPContext接口,这是您的IHTTPContext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM