简体   繁体   English

WP8上的HttpWebRequest.BeginGetResponse

[英]HttpWebRequest.BeginGetResponse on WP8

I have a problem with WebRequest in a PCL (Portable Class Library) again. 我再次在PCL(可移植类库)中遇到WebRequest问题。

I have this piece of code: 我有这段代码:

    static ManualResetEvent allDone = new ManualResetEvent(false);
    static string doc;

    static public void AddAnime(string encodedLogin, string id, int episodes, int status, int score)
    {
        if (string.IsNullOrEmpty(encodedLogin))
        {
            throw new ArgumentException();
        }
        else
        {   
            doc = String.Format("anime_id={0}&status={1}&episodes={2}&score={3}", id, "watching", episodes, score);

            HttpWebRequest request = HttpWebRequest.CreateHttp("http://mal-api.com/animelist/anime");

            request.Method = "POST";
            request.Headers["Authorization"] = encodedLogin;

            request.BeginGetRequestStream(new AsyncCallback(GetRequestCallBack), request);

            allDone.WaitOne();

            s.ToString();
        }
    }

    static private void GetRequestCallBack(IAsyncResult aResult)
    {
        HttpWebRequest request = (HttpWebRequest)aResult.AsyncState;

        Stream postStream = request.EndGetRequestStream(aResult);

        byte[] byteArray = Encoding.UTF8.GetBytes(doc);

        postStream.Write(byteArray, 0, doc.Length);
        postStream.Flush();

        request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
    }

    static private void GetResponseCallback(IAsyncResult asyncResult)
    {
        HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;

        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);

        Stream dataStream = response.GetResponseStream();
        StreamReader streamReader = new StreamReader(dataStream);

        string responseString = streamReader.ReadToEnd();

        //Do something with string here

        allDone.Set();
    }

Now, the problem is, when I call AddAnime in a sample Console app, this runs perfectly fine, but when I call it from my WP8 device or emulator (WXGA), it gets stuck on BeginGetResponse . 现在,问题是,当我在一个示例控制台应用程序中调用AddAnime时,它运行完全正常,但是当我从我的WP8设备或模拟器(WXGA)调用它时,它会卡在BeginGetResponse It doesn't enter GetResponseCallback at all (tested with breakpoints), and doesn't continue with any code directly after BeginGetResponse either. 它根本没有输入GetResponseCallback (使用断点测试),并且在BeginGetResponse之后也不会直接继续使用任何代码。 I've waited for more than two minutes, which should throw a TimeOutException (I think), but even then nothing at all happened. 我等了两分多钟,应该抛出一个TimeOutException (我想),但即便如此也没有发生任何事情。

I tested with: 我测试过:

  • Console app (works) 控制台应用(工作)
  • WP8 WXGA emulator (doesn't work) WP8 WXGA仿真器(不起作用)
  • WP8 Device connected (doesn't work) 连接WP8设备(不起作用)
  • WP8 Device disconnected (doesn't work) WP8设备断开连接(不起作用)

For my PCL I target: 对于我的PCL我的目标:

  • SL4 and higher SL4及更高版本
  • WP7 and higher WP7及更高版本
  • Windows Store Windows应用商店
  • .NET 4.5 .NET 4.5

How could I solve this? 我该怎么解决这个问题? Isn't a PCL used to guarantee cross-compatibility? PCL不是用来保证交叉兼容吗?

Windows Phone (and Silverlight, I think), explicitly prevent you from waiting for the result of a network operation on the UI thread. 我认为Windows Phone(和Silverlight)明确阻止您在UI线程上等待网络操作的结果。 You need to return control of the thread to the caller- if you try to block on it then you'll deadlock. 您需要将线程的控制权返回给调用者 - 如果您尝试阻止它,那么您将陷入僵局。

The easiest way to do this is with Tasks and async/await. 最简单的方法是使用Tasks和async / await。 We've released an Async Targeting Pack which adds this support to the platforms you're targeting (and Portable Class Libraries targeting them). 我们发布了一个Async Targeting Pack ,它将此支持添加到您要定位的平台(以及针对它们的可移植类库)。 Note that Windows Phone 7.0 isn't supported, you'll need to choose Windows Phone 7.1/7.5 (it's the same thing but different places use a different version number). 请注意,不支持Windows Phone 7.0,您需要选择Windows Phone 7.1 / 7.5(它是相同的,但不同的地方使用不同的版本号)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM