【问题标题】:How to Create http Web Request in Windows Phone Silverlight Project?如何在 Windows Phone Silverlight 项目中创建 http Web 请求?
【发布时间】:2014-12-27 10:02:43
【问题描述】:

我想创建一个 Http Post 并从 Windows Phone 应用程序中获得响应...这是我在 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();

但我不能这样做,因为它给出了一个错误,即 GetResponse 方法不能在 Silverlight 项目中使用。有什么替代方法,我该怎么做?

【问题讨论】:

    标签: c# windows silverlight windows-phone-8 windows-phone-8.1


    【解决方案1】:

    大多数导致阻塞行为的方法已从 WP/Silverlight API 中删除(这里的想法是不给开发人员任何机会无意中锁定 UI)。

    同步 IO 属于这一类。

    您需要使用async 方法重写您的方法:

    public async Task<SomeReturnType> MyMethod()
    {
        //...
        HttpWebResponse response = 
            (HttpWebResponse)(await request.GetResponseAsync()); 
        //...
    
    }
    

    【讨论】:

    • 我是一个完整的新手,我不知道如何实现这一点。你能提供我可以阅读更多关于此的链接吗?
    • 我做到了。 async 是上面的链接。它链接在这里:msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx
    • 非常感谢您的帮助。
    • 如何忽略HttpWebRequest中的Https证书?
    • @SunilKumarSC 打开一个问题来问这个怎么样?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多