Fedor Finkenflugel
Fedor Finkenflugel

Reputation: 355

HttpWebRequest.BeginGetResponse on WP8

I have a problem with WebRequest in a PCL (Portable Class Library) again.

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. It doesn't enter GetResponseCallback at all (tested with breakpoints), and doesn't continue with any code directly after BeginGetResponse either. I've waited for more than two minutes, which should throw a TimeOutException (I think), but even then nothing at all happened.

I tested with:

For my PCL I target:

How could I solve this? Isn't a PCL used to guarantee cross-compatibility?

Upvotes: 3

Views: 2738

Answers (1)

Daniel Plaisted
Daniel Plaisted

Reputation: 16744

Windows Phone (and Silverlight, I think), explicitly prevent you from waiting for the result of a network operation on the UI thread. 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. We've released an Async Targeting Pack which adds this support to the platforms you're targeting (and Portable Class Libraries targeting them). 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).

Upvotes: 2

Related Questions