Windows phone 8 从windows phone 8应用程序访问web api 2应用程序

Windows phone 8 从windows phone 8应用程序访问web api 2应用程序,windows-phone-8,asp.net-web-api,Windows Phone 8,Asp.net Web Api,尝试从windows phone 8应用程序访问在本地主机上运行的ASP.NET Web Api 2应用程序时出现问题。我已经尝试过,并寻找了很多例子如何做到这一点,但没有结果。我使用Fiddler测试了我的api,它运行正常。我测试了从web应用程序访问它,它可以工作,但当我尝试从WindowsPhone8应用程序访问它时,它不能工作。你能告诉我如何配置我的WindowsPhone8模拟器来访问它吗?提前感谢您。这是一篇关于在Windows Phone 8中访问web api方法的详细文章

尝试从windows phone 8应用程序访问在本地主机上运行的ASP.NET Web Api 2应用程序时出现问题。我已经尝试过,并寻找了很多例子如何做到这一点,但没有结果。我使用Fiddler测试了我的api,它运行正常。我测试了从web应用程序访问它,它可以工作,但当我尝试从WindowsPhone8应用程序访问它时,它不能工作。你能告诉我如何配置我的WindowsPhone8模拟器来访问它吗?提前感谢您。

这是一篇关于在Windows Phone 8中访问web api方法的详细文章

具体来说,这里是如何从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)
            });
        }
    }
string apirl=@”http://www.contoso.com/api/Books";
WebClient WebClient=新的WebClient();
webClient.Headers[“Accept”]=“application/json”;
webClient.DownloadStringCompleted+=新的DownloadStringCompletedEventHandler(webClient\u DownloadCatalogCompleted);
DownloadStringAsync(新Uri(apiUrl));
private void webClient_DownloadCatalogCompleted(对象发送方,DownloadStringCompletedEventArgs e)
{
尝试
{
this.Items.Clear();
如果(例如,结果!=null)
{
var books=JsonConvert.DeserializeObject(如Result);
int id=0;
foreach(书籍中的书籍详细信息)
{
this.Items.Add(新的ItemViewModel()
{
ID=(ID++).ToString(),
LineOne=书名,
第二行=书。作者,
第三行=book.Description.Replace(“\n”,”)
});
}
this.IsDataLoaded=true;
}
}
捕获(例外情况除外)
{
this.Items.Add(新的ItemViewModel()
{
ID=“0”,
LineOne=“发生错误”,
LineTwo=String.Format(“发生以下异常:{0}”,例如Message),
LineThree=String.Format(“其他内部异常信息:{0}”,ex.InnerException.Message)
});
}
}

希望这有帮助

仿真器是一个虚拟机,因此手机应用程序中的“localhost”指的是仿真器本身,而不是运行web服务的主机。要访问它,您必须在本地网络中提供主机的实际IP地址,而不是“localhost”。通过在cmd控制台中运行ipconfig,您可以看到您的IP是什么。

您做了什么?您正在通过电话访问它。您使用什么主机名/IP访问您的计算机?我想通过emulator访问它,因为我没有带windows phone的电话。您可以使用HttpClient而不是WebClient吗?