无法使用Async(async await)下载JSON

时间:2013-03-04 12:49:21

标签: c# windows-phone-8 async-await

我现在已经挣扎了一段时间,但我无法完成这项工作。我正在尝试使用'sort of'async await将json字符串下载到我的Windows Phone 8应用程序。

我正在使用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"; } }
 }

和我的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();
        }
    }

一旦它点击“HttpWebResponse someResponse =(HttpWebResponse)responseRequest.EndGetResponse(asyncResponse);”,我得到一个WebException:

“System.Net.WebException:远程服务器返回错误:NotFound”

当我深入潜水时,我注意到这个错误不是实际错误。当我检查HttpExtensions类中的GetResponseAsync方法内的“asyncResponse”时,我注意到错误:

“AsyncWaitHandle ='asyncResponse.AsyncWaitHandle'引发了'System.NotSupportedException'类型的异常”

我不知道如何完成这项工作。这是我做错的事吗?

1 个答案:

答案 0 :(得分:1)

我现在看到了问题。由于模拟器使用的是虚拟机,因此无法使用localhost,因为localhost是手机而不是PC。请参阅此解决方案:Windows Phone 8 Emulator: Access localhost