本文介绍了如何在WindowsPhone 8中使用HttpClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我对此事有很大的疑问.我正在尝试在新项目中使用HttpClient方法.我尝试以下代码:

Hey everyone i have a big problem with this thing. i'm trying to use HttpClient method in my new project. i try this code:

        var httpClient = new HttpClient();
        var request = await httpClient.GetAsync(new Uri("http://www.google.com/",                     UriKind.RelativeOrAbsolute));
        var txt = request.Content.ReadAsStringAsync();
        MessageBox.Show(txt.Result);

我认为这是真实的代码,因为我是在控制台应用程序中编写的,而且效果很好.然后我打开一个新的WindowsPhone 8项目并编写此代码.代码不起作用,它返回Null.有时它可以工作,但通常不起作用.我以为我的Visual Studio工作不正常,我删除了它并重新安装了它,但没有任何更改.你觉得怎么样?

i think it is true code because i wrote it in Console app and it works fine. Then i opened a new WindowsPhone 8 project and write this code.And code doesn't work, it returns Null. Sometimes it works but generally not. i thought my Visual Studio wasnt work good and i deleted it and re-install it, and nothing had been changed. what do you think?

推荐答案

尝试一下.

var httpClient = new HttpClient();
        var response = await httpClient.GetAsync(new Uri("http://www.google.com/",                     UriKind.RelativeOrAbsolute));
        response.EnsureSuccessStatusCode();
        var txt = response.Content.ReadAsStringAsync();
        MessageBox.Show(txt.Result);

在行response.EnsureSuccessStatusCode();中创建断点,以确保每次响应httpcode为200.

make the breakpoint in the line response.EnsureSuccessStatusCode(); to make sure response httpcode is 200 every time.

这篇关于如何在WindowsPhone 8中使用HttpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 20:07