locked
Is there any windows 8 httpclient method alternative to windows phone Webclient DownloadStringCompleted method ?

    Question

  • i want to know is there any method similar to DownloadStringCompleted of WebClient for httpclient class in windows 8 store apps ?
    Thursday, August 07, 2014 7:54 AM

Answers

  • The HttpClient class doesn't fire any event when an asynchronous resource-download operation completes, but it has a GetStringAsync method which should be called using the await keyword:

    async void YourMethod()
            {
                HttpClient client = new HttpClient();
                var data = await client.GetStringAsync("your url...");
                
                //code to be executed when the string has been downloaded goes here...
            }

    Using the async/await keywords, you don't have to hook up any event handlers that runs after the asynchronous call has completed but you can simply write asynchronous code almost like you write synchronous code. Please refer to the following page for more information: http://msdn.microsoft.com/en-us/library/hh191443.aspx

    • Marked as answer by kranthi88 Thursday, August 07, 2014 2:25 PM
    Thursday, August 07, 2014 9:06 AM

All replies

  • Like this?

    HttpClient client = new HttpClient();
    HttpResponseMessage responseMessage = await client.SendRequestAsync(request);
    string responseText = await responseMessage.Content.ReadAsStringAsync();
    

    Best,

    Robert


    Changing the world... bit by bit.

    Thursday, August 07, 2014 8:40 AM
  • The HttpClient class doesn't fire any event when an asynchronous resource-download operation completes, but it has a GetStringAsync method which should be called using the await keyword:

    async void YourMethod()
            {
                HttpClient client = new HttpClient();
                var data = await client.GetStringAsync("your url...");
                
                //code to be executed when the string has been downloaded goes here...
            }

    Using the async/await keywords, you don't have to hook up any event handlers that runs after the asynchronous call has completed but you can simply write asynchronous code almost like you write synchronous code. Please refer to the following page for more information: http://msdn.microsoft.com/en-us/library/hh191443.aspx

    • Marked as answer by kranthi88 Thursday, August 07, 2014 2:25 PM
    Thursday, August 07, 2014 9:06 AM
  • thanx a lot magnus, your solution is working flawlessly & your way of explanation is very simple, straight to the point & great!!!! :) 
    Thursday, August 07, 2014 2:25 PM