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

查看:132
本文介绍了在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 ,然后使用 HttpWebRequest <中的 GetResponse()方法获取响应/ 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()似乎不存在 HttpWebRequest WebRequest (这是它的基类)。我唯一的选择是使用 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天全站免登陆