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
In 4.5 (your title says so) you can use async/await
public async void MyMethod()
{
HttpClient client = new HttpClient();
HttpResponseMessage responseMsg = await client.GetAsync("http://www.google.com");
//do your work
}
To download a string you can simply use
public async void Question83()
{
HttpClient client = new HttpClient();
var responseStr = await client.GetStringAsync("http://www.google.com");
MessageBox.Show(responseStr);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With