Reading response from HttpClient.GetStringAsync

I am working on a universal Windows application using the new runtime for Windows Phone / Store applications. I am sending a request to the server using the following code and waiting for an HTML response. However, when I return the string and display it in the user interface, it simply says:

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

This does not show me the actual HTML / XML that should be returned. When I use the same URL in a regular Windows Forms application, it returns the data I expect, but the code I use is different in that Win32 is not WinRT / this new RT.

Here is my code. I suspect that I am not returning the data in the correct format or something like that, but I do not know what I should do.

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;
        }
+2
1

, , . , , (, , HttpClient), .

, https://msdn.microsoft.com/en-us/library/windows/apps/windows.web.http.httpclient.aspx. GetStringAsync . var. checkResult, .

.

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

, , , GetData:

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

, , - , .

+4

All Articles