[英]Can't find HttpWebRequest.GetResponse() in WP7 Project
I'm trying to send a GET request using HttpWebRequest. 我正在尝试使用HttpWebRequest发送GET请求。
I've found a lot of examples all over the web (for example, this one ...just go down to the Scrape()
method). 我在网上找到了很多例子(例如, 这个 ...只是转到Scrape()
方法)。 They all basically do the same thing: 他们基本上都做同样的事情:
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
. 使用WebRequest.Create(URL)创建HttpWebRequest
对象并将其强制转换为HttpWebRequest
,然后使用HttpWebRequest
的GetResponse()
方法获取响应。
Thing is, GetResponse()
doesn't seem to exist in either HttpWebRequest
or WebRequest
(which is its base class). 事实上, GetResponse()
似乎不存在于HttpWebRequest
或WebRequest
(它是它的基类)中。 My only option is to use BeginGetResponse()
. 我唯一的选择是使用BeginGetResponse()
。
The only thing I found is that GetResponse()
is synchronous, while BeginGetResponse()
is asynchronous, and that Silverlight only allows the asynchronous one. 我发现的唯一事情是GetResponse()
是同步的,而BeginGetResponse()
是异步的,而Silverlight只允许异步的。 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. 嗯,这对我没有任何帮助,因为整个事情都是一个XNA项目,这是我在里面创建的一个简单的C#类。
Well to be more accurate, this is a Windows Phone game, created in XNA 4.0 更确切地说,这是一款在XNA 4.0中创建的Windows Phone游戏
HttpWebRequest webRequest = WebRequest.Create(URL) as HttpWebRequest;
StreamReader responseReader = new StreamReader(
webRequest.GetResponse().GetResponseStream());
Does anyone have any idea as to why I don't have GetResponse()
? 有没有人知道为什么我没有GetResponse()
?
XNA 4 for Windows Phone 7 can only make asynchronous calls. 适用于Windows Phone 7的XNA 4只能进行异步调用。 You might find the code at the bottom of this post helpful as well. 您可能会发现在底部的代码这篇文章有帮助。
Code from that post: 该帖子的代码:
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.