Kev*_*sen 35 c# android localhost android-emulator asp.net-web-api
我刚刚将C#Web API组件(Web API模型和控制器)添加到localhost现有项目的副本中.
应该从Android应用程序调用此Web API的GET方法.在这个链接中,它解释了我应该10.0.2.2在Android模拟器上使用它来获取计算机127.0.0.1.
当我这样做时,它在Android应用程序中对我的HttpRequest不起作用.所以我去了Android浏览器并直接键入它,它也没有用.
然后我尝试使用127.0.0.1而不是localhost在我的计算机的浏览器中,并且由于一些未知的原因它也不起作用..是否有不同的localhost和127.0.0.1?我一直认为他们是同一个人.
这是400 error我在使用时获得的127.0.0.1:

并与localhost一切工作正常.
所以,我的问题:我如何使用localhost的仿真器(或者,如何解决使用时,我得到的错误127.0.0.1,而不是localhost)?另外,我想知道的区别localhost和127.0.0.1,因为我一直以为他们是一样的.
在此先感谢您的回复.
编辑1:
在这个stackoverflow问题中,他们提到了System32Windows中的主机文件.我打开用记事本文件++(如管理员),并注释掉线127.0.0.1 localhost和::1 localhost.但不幸的是,这并没有解决问题,我仍然不能127.0.0.1在我的电脑上用作替代品localhost.可能是因为我的问题是相反的(我可以访问localhost,但不是127.0.0.1,而不是相反.)
编辑2:
在这个stackoverflow回答中,它解释了localhost和之间的区别127.0.0.1是:
127.0.0.1 在某些编程语言中,将更容易被识别为IP.localhost可以更改为计算机主机文件中的另一个IP(我的编辑1中提到的文件).所有我现在都明白了这些差异,我只是不明白为什么我的localhost正在工作,但127.0.0.1不是..
编辑3:
是否必须对port(我使用54408as 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=128ping 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
Run Code Online (Sandbox Code Playgroud)
最后两行曾经是:
# 127.0.0.1 localhost
# ::1 localhost
Run Code Online (Sandbox Code Playgroud)
编辑5 /半解决方案:
在Jake C的建议之后,我去寻找Visual Studio端口配置并找到了以下网站.在" 为使用Visual Studio开发服务器的Web应用程序项目指定端口 "部分,我按照说明操作,将项目属性中的Web选项更改为Use Visual Studio Development Servermy 54408 port.
这是朝着正确方向迈出的一大步,因为127.0.0.1:54408主页现在有效.但是,一旦我尝试使用Google OAuthC#网站登录,我就会收到以下错误:

我在C#web项目上工作的一位前同事告诉我一次这个stackoverflow帖子.在这篇文章的答案中声明我应该添加redirect-urls(在我的情况下127.0.0.1)Google APIs Console.现在我无法访问Console我的localhost项目,因为它是通过获得的SVN.我将要求我的一位主管允许查看Google APIs Console这个C#web项目,也许可以编辑redirect-urlsto include 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到重定向的URL.然后在我的C#web项目中,App_Start/AuthConfig.cs我更改了客户端设置以使用这个新客户端.
再次感谢Jake C提出的改变http port configurationsVisual Studio的建议.这对我有用.
Mic*_*wen 14
如果您使用的是Visual Studio的内置Web服务器(IIS Express),则默认情况下会映射localhost; 启用127.0.0.1:
1)在路径:%USERPROFILE%\ Documents\IISExpress\config
2)找到配置文件:applicationhost.config
3) - 在编辑器中打开配置文件(我使用记事本++)
4)搜索站点端口,例如,如果URL通常是localhost:57578,搜索"57578",您应该找到:
<binding protocol="http" bindingInformation="*:57578:localhost" />
Run Code Online (Sandbox Code Playgroud)
5)将此条目更改为:
<binding protocol="http" bindingInformation="*:57578:*" />
Run Code Online (Sandbox Code Playgroud)
6)保存并退出,重启网站.
注意:您需要在创建新虚拟目录(更改端口号Project/Properties/Web/Project Url)时重复此过程,这将在applicationhost.config文件中创建新条目.
Jak*_*e C 11
因此,通常HTTP 400无效主机名错误通常意味着您没有网站集接受所有主机名和/或IP地址.我认为因为这是一个C#应用程序,你在IIS上托管它.要修复此问题,请打开IIS管理器(Win + R并输入inetmgr),然后展开服务器,然后展开"站点",然后右键单击托管应用程序的网站并选择绑定.在此列表中应该有端口54408的http绑定,双击它.在IP地址下,确保选中"所有未分配",并在"主机名"下确保该字段为空.点击OK,然后关闭.该设置应立即生效,无需重置IIS.
如果你只是通过Visual Studio的内置Web部署进行测试,那么在VS的某个地方就有类似的设置(我有点生疏,所以我不记得到底在哪里或如何).或者,您可以在IIS中按照自己的方式设置网站,然后将VS部署到该网站,而不是使用自己的内部服务器.我认为这样做可能会有一些调试方面的缺点(再一次我对细节有点模糊,我会在记住它们时编辑它们或弄清楚它).
一些背景知识,为什么存在这些设置:有时服务器需要托管多个网站,都是通过端口80进行访问,从而让我们说我们有foo.com和bar.com他们太小,以保证得到一个单独的服务器对他们俩的.因此,它们都托管在IP地址为的服务器上1.2.3.4.现在,当您foo.com在浏览器中输入URL 并按下go时,它首先将主机名解析为1.2.3.4,然后创建一个请求,并将该请求的一部分称为主机头.在这种情况下,主机头填充了输入的URL的主机名foo.com.当服务器收到请求时,它会查看主机头并为其提供内容foo.com.
现在,如果您尝试进入1.2.3.4浏览器,它将创建一个带有空白主机标头的请求,因为没有指定.当服务器收到请求时,它不知道该怎么做,因为服务器托管了两个网站,并且没有主机头来指定浏览器正在查找哪个网站,因此它返回错误.
这可能就是你的情况.您的网站托管在localhost主机名下,并且没有回复任何其他请求.我指定要更改的设置基本上告诉服务器无论它进入什么IP地址(网络接口),无论它要查找的主机名是什么,只要它在端口54408上进入,请为此服务网站.
Ham*_*deh 11
在带有IIS express的visual studio 2015中:首先在项目属性中将项目URL从http:// localhost :( Port No)更改为:http://127.0.0.1 :( Port No)
然后单击"创建Visual Directory"
然后转到您的解决方案路径并打开".vs"目录(隐藏目录)并打开"applicationhost.config"
<sites>
<site name="WebSite1" id="1" serverAutoStart="true">
<application path="/">
<virtualDirectory path="/" physicalPath="%IIS_SITES_HOME%\WebSite1" />
</application>
<bindings>
<binding protocol="http" bindingInformation=":8080:localhost" />
</bindings>
</site>
<site name="####" id="2">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="E:\Visual Studio 2015\Projects\####\####" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:5050:localhost" />
</bindings>
</site>
<site name="####(1)" id="3">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="E:\Visual Studio 2015\Projects\####\####" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:5050:127.0.0.1" />
</bindings>
</site>
<siteDefaults>
<logFile logFormat="W3C" directory="%IIS_USER_HOME%\Logs" />
<traceFailedRequestsLogging directory="%IIS_USER_HOME%\TraceLogFiles" enabled="true" maxLogFileSizeKB="1024" />
</siteDefaults>
<applicationDefaults applicationPool="Clr4IntegratedAppPool" />
<virtualDirectoryDefaults allowSubDirConfig="true" />
</sites>
Run Code Online (Sandbox Code Playgroud)
并从*:(端口号)更改您的Web应用程序的最后一个绑定部分:localhost到*:(端口号):127.0.0.1
并停止IIS Express上所有正在运行的项目并重新启动它们.
| 归档时间: |
|
| 查看次数: |
25275 次 |
| 最近记录: |