URL realUrl = new URL("xxx.xxxx.com");
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()));
out.print(param);
out.flush();
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len = 0;
InputStream is = conn.getInputStream();
while ((len = is.read(buf)) != -1) {
System.out.println(len);
baos.write(buf, 0, len);
}
Linux操作系统,Tomcat服务器," xxx.xxxx.com"是一个不正确的网址。
我一直在期待UnknownHostException,但有时我得到的是UnknownHostException。
UnknownHostException错误消息
java.net.UnknownHostException:xxx.xxxx.com at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178) 在java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)at java.net.Socket.connect(Socket.java:579)at sun.net.NetworkClient.doConnect(NetworkClient.java:178)at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
SocketTimeoutException错误消息
java.net.SocketTimeoutException:connect time out out at java.net.PlainSocketImpl.socketConnect(Native Method)at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) 在 java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) 在 java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) 在java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)at java.net.Socket.connect(Socket.java:579)at sun.net.NetworkClient.doConnect(NetworkClient.java:178)at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
答案 0 :(得分:0)
只是猜测 - 有时您的应用无法连接到DNS服务器吗?这可以解释为什么你收到一个SocketTimeoutException而不是一个UnknownHostException - 你的应用程序试图查找主机名但它超时才能确定主机不存在。