【发布时间】:2014-06-17 16:59:19
【问题描述】:
我刚刚将 C# Web API 组件(Web API 模型和控制器)添加到现有项目的 localhost 副本中。
应从 Android 应用调用此 Web API 的 GET 方法。在this link 中解释了我应该在Android Emulator 上使用10.0.2.2 来获取计算机的127.0.0.1。
当我这样做时,它不适用于我在 Android 应用程序中的 HttpRequest。于是我就去安卓浏览器直接输入了,还是不行。
然后我尝试在计算机的浏览器中使用127.0.0.1 而不是localhost,但由于某些未知原因它也不起作用..localhost 和127.0.0.1 之间有什么不同吗?我一直认为他们是一回事。
这是我在使用127.0.0.1 时得到的400 error:
localhost 一切正常。
所以,我的问题是:如何在模拟器上使用localhost(或者,如何解决使用127.0.0.1 而不是localhost 时出现的错误)?另外,我想知道localhost和127.0.0.1之间的区别,因为我一直认为它们是一样的。
提前感谢您的回复。
编辑 1:
在this stackoverflow question 中,他们提到了 Windows 的System32 中的主机文件。我用 Notepad++(以管理员身份)打开了这个文件,并用127.0.0.1 localhost 和::1 localhost 取消了注释。但不幸的是,这并没有解决问题,我仍然不能在我的计算机上使用127.0.0.1 来替代localhost。可能是因为我的问题是相反的(我可以访问localhost,但不能访问127.0.0.1,而不是相反。)
编辑 2:
在this stackoverflow answer 中解释了localhost 和127.0.0.1 之间的区别是:
-
127.0.0.1在某些编程语言中会更容易被识别为 IP。 -
localhost可以更改为计算机主机文件中的另一个 IP(我的 Edit 1 中提到的文件)。 - IPv4 和 IPv6 之间存在一些差异。
我现在有点明白其中的区别了,我只是不明白为什么我的 localhost 可以正常工作,但 127.0.0.1 却不行..
编辑 3:
它是否与port 有关系(我使用54408 作为port)?我打开了cmd 并做了以下测试:
-
ping localhost:我收到了 4 次Reply from ::1: time<1ms的回复。 -
ping 127.0.0.1:我收到了 4 次回复Reply from 127.0.0.1: bytes=32 time<1ms TIL=128 -
ping localhost:54408:我收到一个错误Ping request could not find localhost:54408. Please check the name and try again. -
ping 127.0.0.1:54408:我收到一个错误Ping request could not find 127.0.0.1:54408. Please check the name and try again.
就像我之前说的:浏览器中的127.0.0.1:54408 给出了上图中的错误。 localhost:54408 工作得很好..
仍然没有人知道如何解决这个问题?
编辑 4:
我的hosts-file 的副本,位于C:\Windows\System32\drivers\etc。
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
::1 localhost
最后两行曾经是:
# 127.0.0.1 localhost
# ::1 localhost
编辑 5 / 半解决方案:
在 Jake C 的建议下,我去寻找 Visual Studio 端口配置并找到 the following site。在“为使用 Visual Studio 开发服务器的 Web 应用程序项目指定端口”部分,我按照说明将项目属性中的 Web 选项更改为 Use Visual Studio Development Server,并使用我的 54408 port .
这是朝着正确方向迈出的一大步,因为127.0.0.1:54408 主页现在可以使用。但是,一旦我尝试在 C# 网站上使用Google OAuth 登录,我就会收到以下错误:
我的一位从事 C# Web 项目的前同事曾经告诉我关于 this stackoverflow post 的事。在这篇文章的答案中指出,我应该将redirect-urls(在我的情况下为127.0.0.1)添加到Google APIs Console。
现在我无法访问我的localhost 项目的这个Console,因为它是通过SVN 获得的。我将请求我的一位主管允许查看此 C# Web 项目的 Google APIs Console,并可能编辑 redirect-urls 以包含 127.0.0.1。
一旦我让它完全正常工作,我会接受 Jake C 的回答,因为他对Http port configurations 的解释确实帮助我找到了答案。
编辑 6:
好的,我使用了自己的 Google API 控制台并使用新的客户端 ID 创建了一个新项目。我已将http://localhost:54408/Account/ExternalLoginCallback 和http:127.0.0.1:54408/Account/ExternalLoginCallback 添加到redirect-urls。然后在我的 C# Web 项目的App_Start/AuthConfig.cs 中,我更改了客户端设置以使用这个新客户端。
再次感谢 Jake C 建议您在 Visual Studio 中更改 http port configurations。这对我有用。
【问题讨论】:
-
您是否查看了 Windows32 文件夹中的 hosts 文件,可能那里有问题? localhost 是明确映射到 127.0.0.1 还是您在某个时候删除了它?
-
@Jagga 是的,我做到了(见第一个编辑)。
System32hosts-file中的所有内容都是注释,除了最后两行:127.0.0.1 localhost和::1 localhost。我在编辑 4 中添加了完整的文件。 -
试着把# 也注释到最后两行。在我的 hosts 文件中,他们被评论了,所以我假设他们默认被评论。
-
@Jagga 默认情况下,它们也曾经在我的文件中被注释掉。我确实尝试过像你说的那样再次评论它们,但我仍然得到相同的结果..
-
当某些浏览器看到带有“.”的 IP 地址时,您应该知道的一件事。而不是“localhost”,无论端口如何,它们都不会传递凭据。在 IE 中,当使用 127.0.0.1 地址时,它将不再像以前那样将凭据传递给服务器。因此,使用该环回地址会发生一些奇怪的事情。
标签: c# android asp.net-web-api android-emulator localhost