Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

java.net.UnknownHostException & java.net.ConnectException: Tried all: 1 addresses, but could not con

2947632Oct 2 2017

Previously , we had following code in our application and it used to fetch the required data. We just used to read the required fields by forming a web-service URL by passing username , password and search parameter (DEA number). The same URL (with parameters) could also be hit from browser directly to see the results :

DocumentBuilderFactory factory =
  
DocumentBuilderFactory.newInstance();

  
DocumentBuilder parser = factory.newDocumentBuilder();

  
System.setProperty("http.proxyHost", getProxyHost());
  
System.setProperty("http.proxyPort", getProxyPort());

  
System.out.println("Before URL formation");

  URL url
= new URL(
  
"http://www.deanumber.com/Websvc/deaWebsvc.asmx/GetQuery?UserName=" + getDeaUsername() + "&Password=" + getDeaPassword() + "&DEA=" +
  deaNumber
+
  
"&BAC=&BASC=&ExpirationDate=&Company=&Zip=&State=&PI=&MaxRows=");

  
System.out.println("After URL formation");
  
System.out.println("URL formed is: "+url);
  
Document document = null;
  
try {

  
System.out.println("toExternalForm");

  
String strURL = url.toExternalForm();

  
System.out.println("toExternalForm done: "+strURL);

  
document = parser.parse(strURL); //This is causing UnknownHostException
  
}
  
catch (SAXParseException spe)
  
{  System.out.println("Inside SAXParseException");
  spe
.printStackTrace();
  
}

Now, the web-service URL has got changed from http to https with rest of the things same. The new URL also works from the browser. Moreover, even if I enter the http URL in the browser , it automatically redirects to https and shows the data. However, the above code is not doing the same and it gave following exception : java.net.ProtocolException: Unsupported protocol: https'

So, I changed the code to use https in the url, but now a new exception is coming - java.net.UnknownHostException. From the console log, I can see that the exception is coming at ==>document = parser.parse(strURL). Here , parser is a reference variable for a DocumentBuilderobject as you can see from the code above.

Then, I added the IP address of the website to the hosts file (Windows) as I am testing it currently on my local setup through Eclipse. Now , it's giving below exception: java.net.ConnectException: Tried all: 1 addresses, but could not connect over HTTPS to server: www.deanumber.com port: 443. Also, following code returns False: boolean res = java.net.InetAddress.getByName(url.getHost()).isReachable(10000);

Below is the complete stack trace for UnknownHostException for your reference:

java.net.UnknownHostException: www.deanumber.com
[2017-23-28 08:23,   0]ERROR[[ACTIVE] ExecuteThread: '6' for queue: 'weblogic.kernel.Default (self-tuning)'](DeaFetcher.java:123) - Exception in DeaFetcher.getDeaFromUrl java.net.UnknownHostException: www.deanumber.com
  at java
.net.InetAddress.getAllByName0(InetAddress.java:1250)
  at java
.net.InetAddress.getAllByName(InetAddress.java:1162)
  at java
.net.InetAddress.getAllByName(InetAddress.java:1098)
  at weblogic
.net.http.HttpsClient.openServer(HttpsClient.java:265)
  at weblogic
.net.http.HttpsClient.openServer(HttpsClient.java:350)
  at weblogic
.net.http.HttpsClient.New(HttpsClient.java:553)
  at weblogic
.net.http.HttpsURLConnection.getHttpClient(HttpsURLConnection.java:332)
  at weblogic
.net.http.HttpURLConnection.getInputStream(HttpURLConnection.java:711)
  at weblogic
.net.http.SOAPHttpsURLConnection.getInputStream(SOAPHttpsURLConnection.java:37)
  at com
.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:643)
  at com
.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:189)
  at com
.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Oct 30 2017
Added on Oct 2 2017
0 comments
3,564 views