2

调用此 Web 服务时有时会出现未知主机异常

这是我用来从 Webservices 获取信息的方法:

String response = null;
         URL url;
         HttpsURLConnection con = null;

         try {

            // Create a context that doesn't check certificates.
                SSLContext ssl_ctx = SSLContext.getInstance("TLS");
                TrustManager[ ] trust_mgr = get_trust_mgr();
                ssl_ctx.init(null,                // key manager
                             trust_mgr,           // trust manager
                             new SecureRandom()); // random number generator
                HttpsURLConnection.setDefaultSSLSocketFactory(ssl_ctx.getSocketFactory());

            url = new URL("https://example.org/v1/user/login/");

            HttpsURLConnection con = (HttpsURLConnection)url.openConnection();

            // Guard against "bad hostname" errors during handshake.
                con.setHostnameVerifier(new HostnameVerifier() {
                    public boolean verify(String host, SSLSession sess) {
                        if (host.equals("localhost")) return true;
                        else return false;
                    }
                });

            con.setRequestProperty("Content-Type", "application/json");
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setRequestMethod("PUT");
            PrintStream ps = null;
            ps = new PrintStream(con.getOutputStream());
            ps.print(aLoginJS);
            ps.close();

            //dump all the content
            StringBuffer string = null;
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
            string = new StringBuffer();
            String inputLine = null;
            while ((inputLine = br.readLine()) != null) {
            string.append(inputLine);
            }

            response = string.toString();
            br.close();

         } catch (MalformedURLException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         } catch (NoSuchAlgorithmException e) {
             e.printStackTrace();
         } catch (KeyManagementException e) {
             e.printStackTrace();
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
            if (con != null) {
                con.disconnect();
            }
         }

         return response;

我得到“java.net.UnknownHostException:example.org”。例外:

java.net.UnknownHostException: example.org
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(Unknown Source)
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)

但这种异常并非一直发生,只是偶尔发生,间隔不常发生。使困惑。

我启用了 IPV4 和 IPV6,并且运行的是 Java 1.6 版。

当两者都启用时,JVM 将使用什么 IP 版本?

通过一些谷歌搜索,我发现 JVM 无法在 IPV6 上正常运行,如果是这样,我该如何路由 JVM 以仅使用 IPV4?

此异常是否发生在仅启用 IPV4 的环境中?请详细说明。

任何帮助表示赞赏。

提前致谢..

4

1 回答 1

0

在启动您的应用程序时将此参数添加到命令行:-Djava.net.preferIPv4Stack=true它的目的是不言自明的。

于 2014-07-03T06:40:21.503 回答