public class GeoHelper
{
const string GeoIpUrl = "http://freegeoip.net/json/{0}";
private readonly string _ipAddress = string.Empty;
public GeoHelper(string ip)
{
_ipAddress = ip;
}
public async Task<string> GetGeoAsync()
{
string uri = string.Format(GeoIpUrl, _ipAddress);
var httpClient = new HttpClient();
var response = await httpClient.GetAsync(uri);
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
return content;
}
}
然后我这样称呼它:
[ChildActionOnly]
public ActionResult UserGeo()
{
var ip = RequestHelper.GetClientIpAddress(Request);
var geoHelper = new GeoHelper(ip);
var response = geoHelper.GetGeoAsync();
var result = response.Result;
var resultobj = JsonConvert.DeserializeObject<GeoInfo>(result);
return Content(resultobj.city);
}
在var result = response.Result;
它等待并且永无止境的过程中,我在等待时得到了白发。我有一个小型控制台应用程序,它在那里运行良好。相同的代码。
为什么?我该如何解决?