无法让异步(异步等待)下载的JSON工作
问题描述:
我一直在努力了一段时间,但我无法得到这项工作。我试图通过使用'sort of'async await来下载一个json字符串到我的Windows Phone 8应用程序。我正在使用the promising solution of Matthias Shapiro。无法让异步(异步等待)下载的JSON工作
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);”,我越来越a WebException:
“System.Net.WebException:远程服务器返回错误:NotFound”
当我深入一点时,我注意到这个错误不是实际的错误。当我检查在HttpExtensions类的GetResponseAsync方法里面的“asyncResponse”我注意到该错误:
“AsyncWaitHandle =‘asyncResponse.AsyncWaitHandle’投掷‘System.NotSupportedException’类型的异常”
我没有想法如何获得这项工作。这是我做错了什么吗?
答
我现在看到问题了。由于模拟器正在使用虚拟机,因此localhost不能用于本地主机,而是您的电脑。看到这个解决方案:Windows Phone 8 Emulator: Access localhost
奇怪的是,我使用完全相同,它的工作对我来说很好。你有没有导入这个参考http://nuget.org/packages/Microsoft.Bcl.Async? – robertk 2013-03-04 12:55:09
我一直试图使用或不使用这个软件包,但都返回给我同样的错误...当我尝试这与一个在线的URL,它的作品'有时'...有一些与尝试它本地?我当我用FireFox检查URL时,我得到了我期望的结果......它太奇怪了...... – 2013-03-04 13:05:00
我现在看到了问题。由于模拟器正在使用虚拟机,因此您不能使用本地主机,因为localhsot是手机而不是您的PC。看到这个解决方案:http://*.com/questions/13149304/windows-phone-8-emulator-access-localhost – robertk 2013-03-04 13:06:39