简体   繁体   中英

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:

// 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.
The main difference is that the PostAsync method accepts an HttpContent as the second argument, so my FormUrlEncodedContent does not work. Also I cannot create an IHttpContent with JSON since I need to pass string parameters.

Any thoughts?

Take a look at

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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