我将多部分表单数据发送到我的Web API,如下所示:
string example = "my string";
HttpContent stringContent = new StringContent(example);
HttpContent fileStreamContent = new StreamContent(stream);
using (var client = new HttpClient())
{
using (var content = new MultipartFormDataContent())
{
content.Add(stringContent, "example", "example");
content.Add(fileStreamContent, "stream", "stream");
var uri = "http://localhost:58690/api/method";
HttpResponseMessage response = await client.PostAsync(uri, content);
Run Code Online (Sandbox Code Playgroud)
这是Web API:
[HttpPost]
[Route("api/method")]
public async Task<HttpResponseMessage> Method()
{
// take contents and do something
}
Run Code Online (Sandbox Code Playgroud)
如何在Web API中读取请求体中的字符串和流?