0
    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 OS,Tomcat server,"xxx.xxxx.com" is an incorrect URL.

I am expecting a UnknownHostException all the time, but sometimes what I am getting is UnknownHostException.

UnknownHostException error message

java.net.UnknownHostException: xxx.xxxx.com at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178) at 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 error message

java.net.SocketTimeoutException: connect timed out at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) at 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)

hhhyyq
  • 53
  • 6

1 Answers1

0

Just a guess - is it possible that sometimes your app can not connect to the DNS server? That would explain why you receive a SocketTimeoutException instead of an UnknownHostException - your app is trying to look up the hostname but it times out before it can determine that the host does not exist.

Mark Madej
  • 1,752
  • 1
  • 14
  • 19