0

我有一个字符串和一个图像,我需要使用Windows 运行时中的HttpClientPOST到 Web 服务器。

如何使用HttpClient在一个请求中发布两件事?

4

2 回答 2

1

简单的!尝试这个:

using Windows.Web.Http;
using Windows.Web.Http.Headers;

private async void Foo()
{
    // the image
    var fileStream = await file.OpenReadAsync();
    var streamContent = new HttpStreamContent(fileStream);
    var filename = "myImage.png";

    // the text
    var text = "oompa loompas";
    var stringContent = new HttpStringContent(text);

    // Putting all together.
    var formDataContent = new HttpMultipartFormDataContent();
    formDataContent.Add(streamContent, "myImage", fileName);
    formDataContent.Add(stringContent, "myString");

    // Send it to the server.
    var response = await (new HttpClient()).PostAsync(uri, formDataContent);
}
于 2014-05-24T16:08:25.407 回答
-1

用于发布字符串

HttpClient client = new HttpClient();
HttpContent content = new StringContent(contentstring);
client.PostAsync(url, content);

对于发布图像,将图像转换为流,流转换为字符串,然后是 httpcontent。

于 2014-05-23T09:53:45.977 回答