2

I have the following code. The async call never returns anything. Even for google.com.

try { using ( var client = new HttpClient()) { var response = client.GetAsync("http://www.google.com"); Debug.WriteLine("Coming here1"+response.Result.IsSuccessStatusCode); if (response.Result.IsSuccessStatusCode) { // by calling .Result you are performing a synchronous call Debug.WriteLine("Coming here1"); var responseContent = response.Result.Content; // by calling .Result you are synchronously reading the result string responseString = responseContent.ReadAsStringAsync().Result; //Console.WriteLine(responseString); } else { Debug.WriteLine("else"); } } } catch(Exception e) { Debug.WriteLine(e.ToString()); } } 
2
  • Is there a specific reason you're not using WebClient? Commented Feb 26, 2014 at 17:12
  • NO specfic reason. I heard httpclient is the new one with improvements...I read this stackoverflow.com/questions/14435520/… and followed Commented Feb 26, 2014 at 17:14

2 Answers 2

0

Try This

try{ WebClient wc = new WebClient(); wc.DownloadStringCompleted+= (sender,args) => { Debug.WriteLine(args.results); }; wc.DownloadStringAsync(new Uri(@"http://www.Google.com",UriKind.RelativeOrAbsolute)); } catch(Exception e){ Debug.WriteLine(e.Message); } 
Sign up to request clarification or add additional context in comments.

2 Comments

Sure. Please let me know if you come across any how to use tutorials for Httpclient on phone
But I figured the HttpClient option..here is the working code codeString strResult = await signUpReq.GetStringAsync(finalUri);code
0

You don't appear to be awaiting your Async call.

Try changing var response = client.GetAsync("http://www.google.com"); to var response = await client.GetAsync("http://www.google.com");

Remember to mark your method as async.

you're also blocking on your async call ReadAsStringAsync().Result. As with client.GetAsync, make sure to await the call instead of blocking with Result. This blog post speaks a bit on the topic.

Read up a bit on async/await. You'll love it once you get the hang of it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.