我有一些文件要上传,但有些文件失败了,因为发布是异步的而不是同步的。
我正在尝试将此调用作为同步调用..
我想等待回复。
如何使此调用同步?
static async Task<JObect> Upload(string key, string url, string
sourceFile, string targetFormat)
{
using (HttpClientHandler handler = new HttpClientHandler {
Credentials = new NetworkCredential(key, "")
})
using (HttpClient client = new HttpClient(handler))
{
var request = new MultipartFormDataContent();
request.Add(new StringContent(targetFormat), "target_format");
request.Add(new StreamContent(File.OpenRead(sourceFile)),
"source_file",
new FileInfo(sourceFile).Name);
using (HttpResponseMessage response = await client.PostAsync(url,
request).ConfigureAwait(false))
using (HttpContent content = response.Content)
{
string data = await content.ReadAsStringAsync().ConfigureAwait(false);
return JsonObject.Parse(data);
}
}
}
感谢任何帮助!
最佳答案
改变
await content.ReadAsStringAsync().ConfigureAwait(false)
到
content.ReadAsStringAsync().Result
ReadAsStringAsync 返回一个 Task 对象。该行末尾的“.Result”告诉编译器返回内部字符串。
关于c# - 使 http 客户端同步 : wait for response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31129873/