使HTTP客户端同步:等待响应

编程入门 行业动态 更新时间:2024-10-22 21:22:33
本文介绍了使HTTP客户端同步:等待响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我要上传一些文件,而某些文件失败,因为帖子是异步的而不是同步的。

I have some file to upload and some of the files failed because the post is asynchronous and not synchronous..

我正在尝试将此调用设为同步

I'm trying to make this call as synchronized call..

我要等待响应。

如何使该呼叫成为同步呼叫?

How can I make this call as synchronous?

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告诉编译器返回内部字符串。

the ReadAsStringAsync returns a Task object. the '.Result' in the end of the line tell the compiler to return the inner string.

更多推荐

使HTTP客户端同步:等待响应

本文发布于:2023-11-25 09:12:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629177.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!