如何从 WP7 中的服务器获取响应?

发布于 2024-11-17 10:20:29 字数 863 浏览 4 评论 0原文

我正在尝试使用 WP7 上的 HttpWebRequest 类发送请求,但没有得到任何响应... 这是我的代码:

            InitializeComponent();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com/");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            tbResponse.Text = reader.ReadToEnd();

            // Cleanup the streams and the response.
            reader.Close();
            dataStream.Close();
            response.Close();
            Console.ReadLine();

此外,我使用此扩展:单击此处,但是我在Windows控制台应用程序上测试了它,没有任何问题,所以我认为问题是我对WP7不了解。

I'm trying to send a request with the HttpWebRequest class on WP7, but I don't get any response...
Here is my code:

            InitializeComponent();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com/");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            tbResponse.Text = reader.ReadToEnd();

            // Cleanup the streams and the response.
            reader.Close();
            dataStream.Close();
            response.Close();
            Console.ReadLine();

Moreover, I use this extension: click here, but I tested it on a Windows Console Application and there wasn't any problem, so I think the problem is that I don't know something about WP7.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰◇生如夏花灿烂 2024-11-24 10:20:29

您需要像这样发出异步请求:

var webRequest = (HttpWebRequest)HttpWebRequest.Create(Url);
webRequest.BeginGetResponse(new AsyncCallback(request_CallBack), webRequest );

和响应处理程序:

void request_CallBack(IAsyncResult result)
{
        var webRequest = result.AsyncState as HttpWebRequest;
        var response = (HttpWebResponse)WebRequest.EndGetResponse(result);
        var baseStream = response.GetResponseStream();

            // if you want to read binary response
            using (var reader = new BinaryReader(baseStream))
            {
                DataBytes = reader.ReadBytes((int)baseStream.Length);
            }

            // if you want to read string response
            using (var reader = new StreamReader(baseStream))
            {
                Result = reader.ReadToEnd();
            }
}

这是我开发的一个帮助程序类,用于在开发 Windows Phone 7 应用程序期间处理我的 Web 请求需求:

http://www.manorey.net/mohblog/?p=17&preview=true

You need to make asynchronous requests like this:

var webRequest = (HttpWebRequest)HttpWebRequest.Create(Url);
webRequest.BeginGetResponse(new AsyncCallback(request_CallBack), webRequest );

and the response handler:

void request_CallBack(IAsyncResult result)
{
        var webRequest = result.AsyncState as HttpWebRequest;
        var response = (HttpWebResponse)WebRequest.EndGetResponse(result);
        var baseStream = response.GetResponseStream();

            // if you want to read binary response
            using (var reader = new BinaryReader(baseStream))
            {
                DataBytes = reader.ReadBytes((int)baseStream.Length);
            }

            // if you want to read string response
            using (var reader = new StreamReader(baseStream))
            {
                Result = reader.ReadToEnd();
            }
}

Here is a helper class I developed to handle my web request needs during development of windows phone 7 apps:

http://www.manorey.net/mohblog/?p=17&preview=true

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文