Reading the response from HttpClient.GetStringAsync

irldev

I am working on a Windows Universal app using the new runtime for Windows Phone/Store apps. I am sending a request to a server using the following code and expecting a HTML response back. However when I return the string and display it in the UI, it just says:

"System.Threading.Tasks.Task'1[System.String]"

It's not showing me the actual HTML/XML that should be returned. When I use the same URL in a normal Windows Forms app, it's returning the data I expect but the code I use there is different due to it being Win32 not WinRT/this new RT.

Here's my code. I suspect I am not returning the data in the right format or something but I don't know what I should be doing.

var url = new Uri("http://www.thewebsitehere.com/callingstuff/calltotheserveretc");
var httpClient = new HttpClient();

        try
        {
            var result = await httpClient.GetStringAsync(url);
            string checkResult = result.ToString();
            httpClient.Dispose();
            return checkResult;
        }
        catch (Exception ex)
        {
            string checkResult = "Error " + ex.ToString();
            httpClient.Dispose();
            return checkResult;
        }
Rob Caplan - MSFT

I don't think the problem is in this code snippet but in the caller. I suspect this code is in a method returning a Task (correct so that the caller can wait for this method's HttpClient call to work) but that the caller isn't awaiting it.

The code snippet looks correct and essentially the same as in the docs at https://msdn.microsoft.com/en-us/library/windows/apps/windows.web.http.httpclient.aspx . GetStringAsync returns a Task. The await will handle the Task part and will return a string into var result. If you break inside the function and examine result or checkResult they'll be the desired strings.

The same thing needs to happen with the caller. If this is in a function

Task<string> GetData() 
{
    // your code snippet from the post 
    return checkResult; // string return is mapped into the Task<string>
}

Then it needs to be called with await to get the string rather than the task and to wait for GetData's internal await to finish:

var v = GetData(); // wrong <= var will be type Task<string>
var data = await GetData(); // right <= var will be type string

The only time you wouldn't await the Task is if you need to manipulate the Task itself and not just get the result.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

httpclient.GetStringAsync blocking

From Dev

HttpClient freezes on the GetStringAsync method

From Dev

Handling http response codes in GetStringAsync

From Dev

HttpClient Get images from response

From Dev

Image caching from a HttpClient response

From Dev

HttpClient Get images from response

From Dev

Reading full response from HttpWebResponse

From Java

Decompressing GZip Stream from HTTPClient Response

From Dev

getting the whole response from HttpClient PostMethod

From Dev

Response from System.Net.HttpClient

From Dev

In C#, what is a synchronous alternate for HttpClient.getStringAsync() method?

From Dev

Difference between HttpClient.GetStringAsync and WebClient.DownloadStringAsync

From Dev

Exception handling for httpclient.GetStringAsync(url) async api call

From Dev

IHttpFilter implementation causes access violation for HttpClient::GetStringAsync call

From Dev

Dart programming query : Reading response from GET

From Dev

Javascript Reading JSON result from AJAX response

From Dev

Error reading response length from authentication socket

From Dev

Reading JSON Response from resource stored in app

From Dev

Error in reading json response from http adapter

From Java

Angular 4.3.3 HttpClient : How get value from the header of a response?

From Dev

Differentiate time exceeded from no server response using HttpClient

From Dev

RequestEntityTooLarge response from Web Api when trying to upload a file with HttpClient

From Dev

The HttpClient fails to stop streaming after receiving a response from the server

From Dev

C# Flurl and HttpClient, no response from REST API

From Dev

C# Get specific object from JSON response using HttpClient

From Dev

C# Get specific object from JSON response using HttpClient

From Dev

HttpClient GetAsync response content is different from what Fiddler is giving me

From Dev

Angular5: Return HttpClient response from a service to a Component?

From Dev

How to format data from api response Angular HttpClient

Related Related

  1. 1

    httpclient.GetStringAsync blocking

  2. 2

    HttpClient freezes on the GetStringAsync method

  3. 3

    Handling http response codes in GetStringAsync

  4. 4

    HttpClient Get images from response

  5. 5

    Image caching from a HttpClient response

  6. 6

    HttpClient Get images from response

  7. 7

    Reading full response from HttpWebResponse

  8. 8

    Decompressing GZip Stream from HTTPClient Response

  9. 9

    getting the whole response from HttpClient PostMethod

  10. 10

    Response from System.Net.HttpClient

  11. 11

    In C#, what is a synchronous alternate for HttpClient.getStringAsync() method?

  12. 12

    Difference between HttpClient.GetStringAsync and WebClient.DownloadStringAsync

  13. 13

    Exception handling for httpclient.GetStringAsync(url) async api call

  14. 14

    IHttpFilter implementation causes access violation for HttpClient::GetStringAsync call

  15. 15

    Dart programming query : Reading response from GET

  16. 16

    Javascript Reading JSON result from AJAX response

  17. 17

    Error reading response length from authentication socket

  18. 18

    Reading JSON Response from resource stored in app

  19. 19

    Error in reading json response from http adapter

  20. 20

    Angular 4.3.3 HttpClient : How get value from the header of a response?

  21. 21

    Differentiate time exceeded from no server response using HttpClient

  22. 22

    RequestEntityTooLarge response from Web Api when trying to upload a file with HttpClient

  23. 23

    The HttpClient fails to stop streaming after receiving a response from the server

  24. 24

    C# Flurl and HttpClient, no response from REST API

  25. 25

    C# Get specific object from JSON response using HttpClient

  26. 26

    C# Get specific object from JSON response using HttpClient

  27. 27

    HttpClient GetAsync response content is different from what Fiddler is giving me

  28. 28

    Angular5: Return HttpClient response from a service to a Component?

  29. 29

    How to format data from api response Angular HttpClient

HotTag

Archive