如何在 Windows Phone 8 中将await与 HttpWebRequest 一起使用?
有没有办法让 IAsyncResult 的东西与 await 一起工作?
private async Task<int> GetCurrentTemperature()
{
GeoCoordinate location = GetLocation();
string url = "http://free.worldweatheronline.com/feed/weather.ashx?q=";
url += location.Latitude.ToString();
url += ",";
url += location.Longitude.ToString();
url += "&format=json&num_of_days=1&key=MYKEY";
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.BeginGetResponse(new AsyncCallback(OnGotWebRequest), webRequest);
}
private void OnGotWebRequest(IAsyncResult asyncResult)
{
HttpWebRequest webRequest = (HttpWebRequest)asyncResult.AsyncState;
var httpResponse = (HttpWebResponse)webRequest.EndGetResponse(asyncResult);
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
string responseText = streamReader.ReadToEnd();
}
}
谢谢!