简体   繁体   English

如何在Windows Phone Silverlight项目中创建http Web请求?

[英]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.

相关问题 如何在Windows Phone 8 Silverlight中创建datacontextchanged事件 - How to create datacontextchanged event in windows phone 8 silverlight 如何在Windows Phone Silverlight中HTTP-POST JPG? - How do I HTTP-POST a JPG in Windows Phone Silverlight? 如何为Windows Phone应用程序创建Silverlight 4用户控件? - How to create Silverlight 4 user control for windows phone app? 如何将项目从 Windows Phone 8.1 Silverlight 转换为 Windows Phone 8.1 Runtime - How to convert project from Windows Phone 8.1 Silverlight to Windows Phone 8.1 Runtime 为Web和Windows Phone 7构建Silverlight游戏 - Building Silverlight Games for Web and Windows Phone 7 如何将BitmapImage图像转换为数据,以便可以使用Windows Phone 7通过HTTP POST请求将其上传到Web? - How do I convert a BitmapImage image to data so I can upload it to the web on a HTTP POST request with Windows Phone 7? Windows Phone,多个HTTP请求并行,多少? - Windows Phone, Multiple HTTP request parallel, how many? Silverlight 4项目-Web Request.GetResponse不可用 - Silverlight 4 Project - Web Request.GetResponse Not Available 如何在Windows Phone Silverlight中刷新画布? - How to refresh canvas in Windows Phone Silverlight? Caliburn Micro:如何在 Windows 手机 silverlight 中导航 - Caliburn Micro: how to navigate in Windows phone silverlight
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM