Reputation: 425
Hey everyone i have a big problem with this thing. i'm trying to use HttpClient method in my new project. i try this code:
var httpClient = new HttpClient();
var request = await httpClient.GetAsync(new Uri("http://www.google.com/", UriKind.RelativeOrAbsolute));
var txt = request.Content.ReadAsStringAsync();
MessageBox.Show(txt.Result);
i think it is true code because i wrote it in Console app and it works fine. Then i opened a new WindowsPhone 8 project and write this code.And code doesn't work, it returns Null. Sometimes it works but generally not. i thought my Visual Studio wasnt work good and i deleted it and re-install it, and nothing had been changed. what do you think?
Upvotes: 1
Views: 4940
Reputation: 645
try this.
var httpClient = new HttpClient();
var response = await httpClient.GetAsync(new Uri("http://www.google.com/", UriKind.RelativeOrAbsolute));
response.EnsureSuccessStatusCode();
var txt = response.Content.ReadAsStringAsync();
MessageBox.Show(txt.Result);
make the breakpoint in the line response.EnsureSuccessStatusCode();
to make sure response httpcode is 200 every time.
Upvotes: 1
Reputation: 457382
You shouldn't be calling Result
. Try this instead:
var httpClient = new HttpClient();
var request = await httpClient.GetAsync(new Uri("http://www.google.com/", UriKind.RelativeOrAbsolute));
var txt = await request.Content.ReadAsStringAsync();
MessageBox.Show(txt);
Upvotes: 0