从 HttpClient.GetStringAsync 读取响应 [英] Reading the response from HttpClient.GetStringAsync

查看:20
本文介绍了从 HttpClient.GetStringAsync 读取响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用适用于 Windows Phone/Store 应用程序的新运行时开发 Windows 通用应用程序.我正在使用以下代码向服务器发送请求并期望返回 HTML 响应.但是,当我返回字符串并将其显示在 UI 中时,它只是说:

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]"

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

它没有向我显示应该返回的实际 HTML/XML.当我在普通的 Windows 窗体应用程序中使用相同的 URL 时,它会返回我期望的数据,但我在那里使用的代码有所不同,因为它是 Win32 而不是 WinRT/这个新的 RT.

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;
        }

推荐答案

我认为问题不在于这段代码片段,而在于调用者.我怀疑此代码在返回任务的方法中(正确以便调用者可以等待此方法的 HttpClient 调用工作)但调用者没有等待它.

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.

代码片段看起来正确,并且与 https://msdn.microsoft.com/en-us/library/windows/apps/windows.web.http.httpclient.aspx.GetStringAsync 返回一个任务.await 将处理任务部分并将字符串返回到 var 结果中.如果您在函数内部中断并检查 result 或 checkResult 它们将是所需的字符串.

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>
}

然后需要用 await 调用它来获取字符串而不是任务并等待 GetData 的内部 await 完成:

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

您唯一不会等待 Task 的情况是您需要操作 Task 本身,而不仅仅是获取结果.

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

这篇关于从 HttpClient.GetStringAsync 读取响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆