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

Posted

技术标签:

【中文标题】使http客户端同步:等待响应【英文标题】:Make http client synchronous: wait for response 【发布时间】:2015-09-16 18:06:00 【问题描述】:

我有一些文件要上传,但有些文件失败了,因为帖子是异步的而不是同步的..

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

我想等待回复。

我怎样才能使这个调用同步?

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);
        
    

任何帮助表示赞赏!

【问题讨论】:

您能否发布有关失败的更多详细信息?异步执行不应导致上传失败。如果你真的必须,你可以使用 Task.Wait 并检查返回值以查看任务是否完成。 msdn.microsoft.com/en-us/library/dd235606(v=vs.110).aspx 我认为您不应该使用 ConfigureAwait。请在不使用 ConfigureAwait 的情况下进行检查。 @rikkigibson 在来自线程池的原始线程上的await 之后继续(没有正确配置 HttpContext/Culture 或 UI 线程)通常会在方法的调用者中处理 NRE(调用代码、错误和框架都没有)由 OP 显示,很难查明问题)。正如 Amit 指出的那样,ConfigureAwait 通常是让代码工作不正常的方法(希望能解决其他一些同步问题)。 IE。这是关于 ASP.Net 使用它的讨论 - ***.com/questions/13489065/…。 没有configureAwait=false,上传没有完成。所以看了这篇博文,加了:blog.stephencleary.com/2012/07/dont-block-on-async-code.html @AlonShmiel:需要明确的是,上传失败并不是因为它是异步的,而是因为上游的代码阻塞了它。 ConfigureAwait(false) 只是一个 hacky 解决方法;正确的解决方法是将调用代码更改为异步而不是阻塞。 【参考方案1】:

应该这样做:

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))

        using (HttpContent content = response.Content)
        
            string data = await content.ReadAsStringAsync();
            return JsonObject.Parse(data);
        
    

【讨论】:

没有configureAwait(false),上传没有完成..所以看了这篇博文,加了:blog.stephencleary.com/2012/07/dont-block-on-async-code.html【参考方案2】:

改变

await content.ReadAsStringAsync().ConfigureAwait(false)

content.ReadAsStringAsync().Result

ReadAsStringAsync 返回一个 Task 对象。行尾的 '.Result' 告诉编译器返回内部字符串。

【讨论】:

我正要放弃才找到这个答案

以上是关于使http客户端同步:等待响应的主要内容,如果未能解决你的问题,请参考以下文章

响应时间高与排队相比

使 Java 客户端等待直到收到来自服务器的响应的程序

近日学习概要

5.Ajax

Ajax

Spring整合JMS(消息中间件)