2

An unknownhost exception sometimes occurs when I call this webservice

This is the method I used to fetch the info from 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;

I get "java.net.UnknownHostException: example.org". Exception:

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)

But this exception does not occur all the time, only sometimes, at infrequent intervals. Confused.

I have IPV4 and IPV6 enabled and I am running Java version 1.6.

What IP version will the JVM use when both are enabled?

By some googling I found that the JVM does not run properly with IPV6, if so, how can I route the JVM to use only IPV4?

Does this exception occur in an IPV4 only enabled environment? Please elaborate.

Any help is appreciated.

Thanks in advance..

Shakthi
  • 175
  • 2
  • 13

1 Answers1

0

Add this parameter to the command line when starting your app: -Djava.net.preferIPv4Stack=true It's purpose is pretty self explanatory.

StephaneM
  • 4,779
  • 1
  • 16
  • 33
  • 1
    I tried your suggestion, but the exception still occurs. Also, please see my updated question. – Shakthi Nov 28 '14 at 07:08