C#中:等待请求完成在C#4.5 [英] C#: Waiting for a request to finish in C# 4.5

查看:231
本文介绍了C#中:等待请求完成在C#4.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的asp.net 4.0应用程序中的HTTP请求。我想对于要等待的线程继续之前。

  HttpClient的客户端=新的HttpClient(); 
HttpResponseMessage responseMsg = client.GetAsync(requesturl)。结果;

//我想等到完成。

responseMsg.EnsureSuccessStatusCode();
任务<串GT; responseBody = responseMsg.Content.ReadAsStringAsync();


解决方案

呼叫在responseBody任务.Wait()


I have a http request in my asp.net 4.0 application. I would like for the thread to wait before it continues on.

HttpClient client = new HttpClient();
HttpResponseMessage responseMsg = client.GetAsync(requesturl).Result;

// I would like to wait till complete.

responseMsg.EnsureSuccessStatusCode();
Task<string> responseBody = responseMsg.Content.ReadAsStringAsync();

解决方案

Call .Wait() on the responseBody Task

这篇关于C#中:等待请求完成在C#4.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆