0

I've been struggling for a while now, but I just can't get this work. I'm trying to download a json string to my Windows Phone 8 application, by using the 'sort of' async await.

I'm using the promising solution of Matthias Shapiro.

HttpExtensions.cs

 public static class HttpExtensions
 {
    public static Task<Stream> GetRequestStreamAsync(this HttpWebRequest request)
    {
        var taskComplete = new TaskCompletionSource<Stream>();
        request.BeginGetRequestStream(ar =>
        {
            Stream requestStream = request.EndGetRequestStream(ar);
            taskComplete.TrySetResult(requestStream);
        }, request);
        return taskComplete.Task;
    }


    public static Task<HttpWebResponse> GetResponseAsync(this HttpWebRequest request)
    {
        var taskComplete = new TaskCompletionSource<HttpWebResponse>();
        request.BeginGetResponse(asyncResponse =>
        {
            try
            {
                HttpWebRequest responseRequest = (HttpWebRequest)asyncResponse.AsyncState;
                HttpWebResponse someResponse = (HttpWebResponse)responseRequest.EndGetResponse(asyncResponse);
                taskComplete.TrySetResult(someResponse);
            }
            catch (WebException webExc)
            {
                HttpWebResponse failedResponse = (HttpWebResponse)webExc.Response;
                taskComplete.TrySetResult(failedResponse);
            }
        }, request);
        return taskComplete.Task;
    }
 }


 public static class HttpMethod
 {
    public static string Head { get { return "HEAD"; } }
    public static string Post { get { return "POST"; } }
    public static string Put { get { return "PUT"; } }
    public static string Get { get { return "GET"; } }
    public static string Delete { get { return "DELETE"; } }
    public static string Trace { get { return "TRACE"; } }
    public static string Options { get { return "OPTIONS"; } }
    public static string Connect { get { return "CONNECT"; } }
    public static string Patch { get { return "PATCH"; } }
 }

And My MainPageViewModel.cs

    protected override void OnActivate()
    {
        base.OnActivate();
        GetSessions();
    }

    private async void GetSessions()
    {
        var result = await GetMyData("http://localhost/api/MyData");
     }

    public async Task<string> GetMyData(string urlToCall)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlToCall);
        request.Method = HttpMethod.Get;
        HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
        using (var sr = new StreamReader(response.GetResponseStream()))
        {
            return sr.ReadToEnd();
        }
    }

Once it hits the "HttpWebResponse someResponse = (HttpWebResponse)responseRequest.EndGetResponse(asyncResponse);", I'm getting a WebException:

"System.Net.WebException: The remote server returned an error: NotFound"

When I dive a bit deeper, I notice this error isn't the actual error. When I check the "asyncResponse" inside the GetResponseAsync method in the HttpExtensions class I notice the error:

"AsyncWaitHandle = 'asyncResponse.AsyncWaitHandle' threw an exception of type 'System.NotSupportedException'"

I have no idea how to get this work. Is it something I'm doing wrong?

svick
  • 236,525
  • 50
  • 385
  • 514
pazcal
  • 929
  • 5
  • 26
  • Weird, I use the exact same and its working fine for me. Have you imported this reference http://nuget.org/packages/Microsoft.Bcl.Async ? – robertk Mar 04 '13 at 12:55
  • I've been trying this with or without this package, but both returns me the same error... When I'm trying this with an online URL, it works 'sometimes'... Has it something to do with trying it locally? I When I'm checking the url with FireFox, Im getting the result I expect... It so weird... – pazcal Mar 04 '13 at 13:05
  • 4
    I see the problem now. Since the emulator is using a virtual machine you cannot use localhost because the localhsot is the phone and not your PC. See this solution: http://stackoverflow.com/questions/13149304/windows-phone-8-emulator-access-localhost – robertk Mar 04 '13 at 13:06
  • Brilliant! All the time, I was thinking I was doing something wrong in code. This makes it much more clear. Thnx! – pazcal Mar 04 '13 at 13:09
  • @robertftw You should post that as an answer. – svick Mar 04 '13 at 16:32
  • Alright done. @PazcaldeJonge feel free to mark it as the answer if it solved your problem! – robertk Mar 04 '13 at 16:39

1 Answers1

1

I see the problem now. Since the emulator is using a virtual machine you cannot use localhost because localhost is the phone and not your PC. See this solution: Windows Phone 8 Emulator: Access localhost

Community
  • 1
  • 1
robertk
  • 2,461
  • 1
  • 27
  • 39