java.net.UnknownHostException:服务器主机名无效:本地
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6484275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.net.UnknownHostException: Invalid hostname for server: local
提问by Ashish Agarwal
What are the steps I should take to solve the error:
我应该采取哪些步骤来解决错误:
java.net.UnknownHostException: Invalid hostname for server: local
I added the new virtual host name at Android emulator but the result returns to
我在 Android 模拟器上添加了新的虚拟主机名,但结果返回
java.net.UnknownHostException virtualhostname at
java.net.InetAddress.lookUpHostByName(InetAddress.java:506)
When I type my virtualhost URL on my PC, it works on display. Then again, when I ran on Emulator and check on Logcat, I couldn't be able to read or check the HTTP status if 200, 202, or an error code number. It simply returned to UnknownHostException
当我在我的 PC 上输入我的虚拟主机 URL 时,它可以正常显示。然后,当我在 Emulator 上运行并检查 Logcat 时,如果 200、202 或错误代码号,我将无法读取或检查 HTTP 状态。它只是回到UnknownHostException
采纳答案by André Laszlo
What the exception is really saying is that there is no known server with the name "local". My guess is that you're trying to connect to your local computer. Try with the hostname "localhost"
instead, or perhaps 127.0.0.1
or ::1
(the last one is IPv6).
例外真正说的是没有名为“本地”的已知服务器。我的猜测是您正在尝试连接到本地计算机。尝试使用主机名"localhost"
,或者127.0.0.1
或者::1
(最后一个是 IPv6)。
From the javadocs:
从javadocs:
Thrown to indicate that the IP address of a host could not be determined.
抛出表示无法确定主机的 IP 地址。
127.0.0.1
or ::1
or "localhost"
should always be the loopback interface, so if that doesn't work I'd be really surprised.
127.0.0.1
或者::1
或"localhost"
应该始终是环回接口,所以如果这不起作用,我会很惊讶。
If there really is a server called "local" on your network - examine your DNS settings or add it to your hosts file.
如果您的网络上确实有一个名为“本地”的服务器 - 检查您的 DNS 设置或将其添加到您的主机文件中。
回答by Ash
java.net.UnknownHostException: Host is unresolved:
java.net.UnknownHostException:主机未解析:
Thrown to indicate that the IP address of a host could not be determined.
抛出表示无法确定主机的 IP 地址。
This exception is also raised when you are connected to a valid wifi but router does not receive the internet. Its very easy to reproduce this:
当您连接到有效的 wifi 但路由器未接收互联网时,也会引发此异常。很容易重现这个:
- Connect to a valid wifi
- Now remove the cable from the router while router is pluged-in
- 连接到有效的 wifi
- 现在在插入路由器时从路由器上拔下电缆
You will observe this error!!
你会观察到这个错误!!
You can't really solve this, You can only notify the user gracefully. (something like - "Unable to make a connection")
你不能真正解决这个问题,你只能优雅地通知用户。(类似于 - “无法建立连接”)
回答by joshwayne diestro
Trying to connect to your local computer.try with the hostname "localhost" instead or perhaps ::/ - the last one is ipv6
尝试连接到您的本地计算机。尝试使用主机名“localhost”或 ::/ - 最后一个是 ipv6
回答by Sidharth N. Kashyap
This might happen due to various reasons
这可能由于各种原因而发生
1) Check if you have VPN connected, you might get this error sometimes if yes
1) 检查您是否已连接 VPN,如果是,有时您可能会收到此错误
"Your hostname, localhost resolves to a loopback address: 127.0.0.1; using 10.xxx.1.193 instead (on interface cscotun0)"
“您的主机名,localhost 解析为环回地址:127.0.0.1;使用 10.xxx.1.193 代替(在接口 cscotun0 上)”
2) Check your $HOSTNAME
2) 检查您的 $HOSTNAME
3) try to ping $HOSTNAME on commandline and if it doesnt work, tweak the system settings to make your local host respond to pings
3) 尝试在命令行上 ping $HOSTNAME,如果它不起作用,请调整系统设置以使您的本地主机响应 ping
回答by Shobhit Puri
I was having the same issue on my mac. I found the issue when I pinged my $HOSTNAME
from terminal and it returned ping: cannot resolve myHostName: Unknown host
.
我在我的 mac 上遇到了同样的问题。当我$HOSTNAME
从终端ping 我的并返回时,我发现了这个问题ping: cannot resolve myHostName: Unknown host
。
To resolve:
解决:
- Do
echo $HOSTNAME
on your terminal. - Whatever hostname it shows (lets say
myHostName
), try to ping it :ping myHostName
. If it returnsping: cannot resolve myHostName: Unknown host
then add an entry into your/etc/hosts
file. For that edit
/etc/hosts
file and add following:127.0.0.1 myHostName
- 做
echo $HOSTNAME
你的终端上。 - 无论它显示什么主机名(可以说
myHostName
),请尝试对其进行 ping 操作:ping myHostName
。如果它返回,ping: cannot resolve myHostName: Unknown host
则在您的/etc/hosts
文件中添加一个条目。 对于该编辑
/etc/hosts
文件并添加以下内容:127.0.0.1 myHostName
Hope it helps.
希望能帮助到你。
回答by matrixanomaly
This is not specific to the question, but this question showed up when I was Googling for the mentioned UnknownHostException
, and the fix is not found anywhere else so I thought I'd add an answer here.
这不是特定于问题的,但是当我在谷歌上搜索提到的问题时出现了这个问题UnknownHostException
,并且在其他任何地方都找不到修复程序,所以我想我会在这里添加一个答案。
The exception that was continuously received was:
不断收到的异常是:
java.net.UnknownHostException: google.com
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
...
No matter how I tried to connect to any valid host, printing it in the terminal would not help either. Everything was right.
无论我如何尝试连接到任何有效主机,在终端中打印它也无济于事。一切正常。
The Solution
解决方案
Not callingtrim()
for the host string which contained whitespace. In writing a proxy server the host was obtained from HTTP headers with the use of split(":")
by semicolons for the HOST
header. This left whitespace, and causes the UnknownHostException
as a host with whitespace is not a valid host. Doing a host = host.trim()
on the String host
solved the ambiguous issue.
不调用trim()
包含空格的主机字符串。在编写代理服务器时,主机是从 HTTP 标头中获得的,标头使用split(":")
分号HOST
。这留下了空格,并导致UnknownHostException
作为带有空格的主机不是有效主机。做一个host = host.trim()
就String host
解决了这个模棱两可的问题。
回答by MacGyver
Your hostname is missing. JBoss uses this environment variable ($HOSTNAME) when it connects to the server.
您的主机名丢失。JBoss 在连接到服务器时使用此环境变量 ($HOSTNAME)。
[root@xyz ~]# echo $HOSTNAME
xyz
[root@xyz ~]# ping $HOSTNAME
ping: unknown host xyz
[root@xyz ~]# hostname -f
hostname: Unknown host
There are dozens of things that can cause this. Please comment if you discover a new reason.
导致这种情况的原因有很多。如果您发现新的原因,请发表评论。
For a hack until you can permanently resolve this issue on your server, you can add a line to the endof your /etc/hosts file:
对于黑客,直到您可以在服务器上永久解决此问题,您可以在 /etc/hosts 文件的末尾添加一行:
127.0.0.1 xyz.xxx.xxx.edu xyz
回答by Jyoti Gupta
Please try to set SPARK_LOCAL_IP environment variable to the ip address(can be localhost i.e. your own ip address) you want to connect. E.g.
请尝试将 SPARK_LOCAL_IP 环境变量设置为您要连接的 ip 地址(可以是 localhost 即您自己的 ip 地址)。例如
$ export SPARK_LOCAL_IP=182.95.208.152
$ export SPARK_LOCAL_IP=182.95.208.152
This way you will not be required to alter existing linux settings. Worked for me, hope helps you too.
这样您就不需要更改现有的 linux 设置。为我工作,希望对你也有帮助。
回答by user8326951
If your /etc/localhosts file has entry as below: Add hostname entry as below:
如果您的 /etc/localhosts 文件具有如下条目: 添加如下主机名条目:
127.0.0.1 local host (add your hostname here)
::1 (add hostname here) (the last one is IPv6).
This should resolve the issue.
这应该可以解决问题。
回答by rex roy
Try the following :
尝试以下操作:
String url = "http://www.google.com/search?q=java";
URL urlObj = (URL)new URL(url.trim());
HttpURLConnection httpConn =
(HttpURLConnection)urlObj.openConnection();
httpConn.setRequestMethod("GET");
Integer rescode = httpConn.getResponseCode();
System.out.println(rescode);
Trim() the URL
修剪() URL