Pergunta

I'm starting windows phone 8 development on my Windows 8 machine and test on the emulator that comes with visual studio 2012. I have a main page with one button on it. Upon pressing the button, it makes a http request.

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    string uriString = "http://209.143.33.109/mjpg/video.mjpg?camera=1";
    var uri = new Uri(uriString);
    var httpWebRequest = HttpWebRequest.Create(uri);

    httpWebRequest.BeginGetResponse(new AsyncCallback(OnGettingResponse), httpWebRequest);
}

private void OnGettingResponse(IAsyncResult ar)
{
    var req = ar.AsyncState as HttpWebRequest;
    var response = (HttpWebResponse)req.EndGetResponse(ar);
    var responseStream = response.GetResponseStream();
}

I set a breakpoint on OnGettingResponse. But when I press the button, the breakpoint is never hit.

Am I missing anything obvious here?

Foi útil?

Solução

I was facing the same issue that my callback was not being called even after waiting too long.

I found this stackoverflow answer https://stackoverflow.com/a/15041383 and modified my code accordingly. The actual problem in that question was that asker wants to have some timeout functionality in the HttpWebRequest under Windows Phone 8. Having timer was irrelevant for me, so I just took the following part of code:

public async Task<string> httpRequest(HttpWebRequest request)
{
    string received;

    using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
    {
        using (var responseStream = response.GetResponseStream())
        {
            using (var sr = new StreamReader(responseStream))
            {

                received = await sr.ReadToEndAsync();
            }
        }
    }

    return received;
}

and modified my call to this method as:

var response = await httpRequest(request);

This is now working fine for me.

I also double checked that ID_CAP_NETWORKING is check in WMAppManifest.xml and internet is working on my emulator.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top