当我试图从windows phone 8应用程序访问在本地主机上运行的ASP.NET WebApi2应用程序时,我遇到了一个问题。我已经尝试并寻找了很多如何做到这一点的例子,但都没有结果。我使用Fiddler测试了我的api,它工作正常。我测试过从web应用程序访问它,它可以工作,但当我试图从windows phone8应用程序访问它时,它不能。你能告诉我如何配置我的windows phone8模拟器来访问它吗?提前谢谢你。
发布于 2014-06-03 19:41:47
仿真器是一个虚拟机,所以手机应用程序中的"localhost“是指仿真器本身,而不是你运行web服务的主机。要访问它,您必须提供本地网络中主机计算机的实际IP地址,而不是"localhost“。您可以在cmd控制台运行ipconfig来查看您的IP地址。
发布于 2014-06-03 03:51:35
以下是有关在Windows Phone 8中访问web api方法的详细文章
Calling Web API from a Windows Phone 8
具体来说,这里是如何从web API获取数据的
string apiUrl = @"http://www.contoso.com/api/Books";
WebClient webClient = new WebClient();
webClient.Headers["Accept"] = "application/json";
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadCatalogCompleted);
webClient.DownloadStringAsync(new Uri(apiUrl));
private void webClient_DownloadCatalogCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
this.Items.Clear();
if (e.Result != null)
{
var books = JsonConvert.DeserializeObject<BookDetails[]>(e.Result);
int id = 0;
foreach (BookDetails book in books)
{
this.Items.Add(new ItemViewModel()
{
ID = (id++).ToString(),
LineOne = book.Title,
LineTwo = book.Author,
LineThree = book.Description.Replace("\n", " ")
});
}
this.IsDataLoaded = true;
}
}
catch (Exception ex)
{
this.Items.Add(new ItemViewModel()
{
ID = "0",
LineOne = "An Error Occurred",
LineTwo = String.Format("The following exception occured: {0}", ex.Message),
LineThree = String.Format("Additional inner exception information: {0}", ex.InnerException.Message)
});
}
}希望这能有所帮助
https://stackoverflow.com/questions/24001657
复制相似问题