在 WP7 项目中找不到 HttpWebRequest.GetResponse() [英] Can't find HttpWebRequest.GetResponse() in WP7 Project

查看:25
本文介绍了在 WP7 项目中找不到 HttpWebRequest.GetResponse()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 HttpWebRequest 发送 GET 请求.
我在网上找到了很多例子(例如,这个...只需转到 Scrape() 方法).他们基本上都做同样的事情:

I'm trying to send a GET request using HttpWebRequest.
I've found a lot of examples all over the web (for example, this one...just go down to the Scrape() method). They all basically do the same thing:

使用 WebRequest.Create(URL) 创建一个 HttpWebRequest 对象并将其转换为 HttpWebRequest,然后使用 GetResponse() 获取响应来自 HttpWebRequest 的 code> 方法.

Create a HttpWebRequest object by using WebRequest.Create(URL) and casting it to HttpWebRequest, then getting the response by using the GetResponse() method from HttpWebRequest.

事情是,GetResponse() 似乎不存在于 HttpWebRequestWebRequest(这是它的基类).我唯一的选择是使用 BeginGetResponse().

Thing is, GetResponse() doesn't seem to exist in either HttpWebRequest or WebRequest (which is its base class). My only option is to use BeginGetResponse().

我发现的唯一一件事是 GetResponse() 是同步的,而 BeginGetResponse() 是异步的,而 Silverlight 只允许异步的.嗯,这对我没有任何帮助,因为整个项目是一个 XNA 项目,这是我在里面创建的一个简单的 C# 类.
更准确地说,这是一个 Windows Phone 游戏,在 XNA 4.0 中创建

The only thing I found is that GetResponse() is synchronous, while BeginGetResponse() is asynchronous, and that Silverlight only allows the asynchronous one. Well, that doesn't help me at all, since the whole thing is an XNA project, and this is a simple C# class I created inside.
Well to be more accurate, this is a Windows Phone game, created in XNA 4.0

HttpWebRequest webRequest = WebRequest.Create(URL) as HttpWebRequest; 
StreamReader responseReader = new StreamReader( 
         webRequest.GetResponse().GetResponseStream());

有人知道为什么我没有 GetResponse() 吗?

Does anyone have any idea as to why I don't have GetResponse()?

推荐答案

XNA 4 for Windows Phone 7 只能进行异步调用.您可能会发现这篇文章底部的代码也很有帮助.

XNA 4 for Windows Phone 7 can only make asynchronous calls. You might find the code at the bottom of this post helpful as well.

来自该帖子的代码:

protected override void Initialize()
{
    string webServiceAddress = @"http://localhost/service/service1.asmx";           
    string methodName = "HelloWorld";

    string webServiceMethodUri = string.Format("{0}/{1}", webServiceAddress, methodName);

    HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(webServiceMethodUri);
    httpWebRequest.Method = "POST";

    httpWebRequest.BeginGetResponse(Response_Completed, httpWebRequest);

    base.Initialize();
 }

 void Response_Completed(IAsyncResult result)
 {
    HttpWebRequest request = (HttpWebRequest)result.AsyncState;
    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);

    using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
    {
        string xml = streamReader.ReadToEnd();

        using(XmlReader reader = XmlReader.Create(new StringReader(xml)))
        {
             reader.MoveToContent();
             reader.GetAttribute(0);
             reader.MoveToContent();
             message = reader.ReadInnerXml();
        }
    }
 }

这篇关于在 WP7 项目中找不到 HttpWebRequest.GetResponse()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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