[英]How to Create http Web Request in Windows Phone Silverlight Project?
I want to make an Http Post and get response from Windows Phone App...Here is how I would have done it in ASP.net. 我想发表一个Http Post并从Windows Phone App获得响应...这就是我在ASP.net中要做的事情。
string strUrl = "http://.....";
WebRequest request = HttpWebRequest.Create(strUrl);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream s = (Stream)response.GetResponseStream();
StreamReader readStream = new StreamReader( s );
string dataString = readStream.ReadToEnd();
response.Close();
s.Close();
readStream.Close();
But I cannot do this as it gives an error that GetResponse method cannot be Used in Silverlight Project. 但是我不能这样做,因为它给出了一个错误,即不能在Silverlight Project中使用GetResponse方法。 What is an alternative to this and how do I do it? 有什么替代方法,我该怎么做?
Most methods that cause blocking behaviour have been eliminated from the WP/Silverlight APIs (the idea here is not to give the developer any opportunity to inadvertently lock up the UI). 导致阻塞行为的大多数方法已从WP / Silverlight API中消除(此处的想法是不给开发人员任何无意中锁定UI的机会)。
Synchronous IO falls into this category. 同步IO属于此类。
You need to rewrite your method using async
methods: 您需要使用async
方法重写您的方法:
public async Task<SomeReturnType> MyMethod()
{
//...
HttpWebResponse response =
(HttpWebResponse)(await request.GetResponseAsync());
//...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.