做多部分时如何从httpclient c#获取响应正文
How to get response body from httpclient c# when doing multipart
我正在尝试 post 使用 System.Net.Http.HttpClient 的多部分数据,
获得的响应是 200 ok.
这是我使用的方法:
public async Task postMultipart()
{
var client = new HttpClient();
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "multipart/form-data");
// This is the postdata
MultipartFormDataContent content = new MultipartFormDataContent( );
content.Add(new StringContent("12", Encoding.UTF8), "userId");
content.Add(new StringContent("78", Encoding.UTF8), "noOfAttendees");
content.Add(new StringContent("chennai", Encoding.UTF8), "locationName");
content.Add(new StringContent("32.56", Encoding.UTF8), "longitude");
content.Add(new StringContent("32.56", Encoding.UTF8), "latitude");
Console.Write(content);
// upload the file sending the form info and ensure a result.
// it will throw an exception if the service doesn't return a valid successful status code
await client.PostAsync(fileUploadUrl, content)
.ContinueWith((postTask) =>
{
postTask.Result.EnsureSuccessStatusCode();
});
}
- 如何从该方法获取响应体?
提示:您正在调用 PostAsync
,并等待结果...但随后没有对其进行任何操作。当您处于异步世界并且可以简单地处理它时,您也不清楚为什么要使用 ContinueWith
:
var response = await client.PostAsync(fileUploadUrl, content);
response.EnsureSuccessStatusCode();
// Now do anything else you want to with response,
// e.g. use its Content property
为了呼应 Jon(不是简单地不同意 Jon),不要将 async/await 世界与 async/await (ContinueWith
) 之前的世界混为一谈。
要将响应正文作为字符串获取,您需要第二次等待:
var response = await client.PostAsync(fileUploadUrl, content);
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
我正在尝试 post 使用 System.Net.Http.HttpClient 的多部分数据, 获得的响应是 200 ok.
这是我使用的方法:
public async Task postMultipart()
{
var client = new HttpClient();
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "multipart/form-data");
// This is the postdata
MultipartFormDataContent content = new MultipartFormDataContent( );
content.Add(new StringContent("12", Encoding.UTF8), "userId");
content.Add(new StringContent("78", Encoding.UTF8), "noOfAttendees");
content.Add(new StringContent("chennai", Encoding.UTF8), "locationName");
content.Add(new StringContent("32.56", Encoding.UTF8), "longitude");
content.Add(new StringContent("32.56", Encoding.UTF8), "latitude");
Console.Write(content);
// upload the file sending the form info and ensure a result.
// it will throw an exception if the service doesn't return a valid successful status code
await client.PostAsync(fileUploadUrl, content)
.ContinueWith((postTask) =>
{
postTask.Result.EnsureSuccessStatusCode();
});
}
- 如何从该方法获取响应体?
提示:您正在调用 PostAsync
,并等待结果...但随后没有对其进行任何操作。当您处于异步世界并且可以简单地处理它时,您也不清楚为什么要使用 ContinueWith
:
var response = await client.PostAsync(fileUploadUrl, content);
response.EnsureSuccessStatusCode();
// Now do anything else you want to with response,
// e.g. use its Content property
为了呼应 Jon(不是简单地不同意 Jon),不要将 async/await 世界与 async/await (ContinueWith
) 之前的世界混为一谈。
要将响应正文作为字符串获取,您需要第二次等待:
var response = await client.PostAsync(fileUploadUrl, content);
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();