我有一个要求,我需要测试tomcat URL是否有效。 为此,我编写了一个Java代码来检查响应代码,并根据该代码确定网站是关闭还是打开。
问题是,URL正在使用HTTPS
并且我收到以下错误:
javax.net.ssl.SSLHandshakeException:
sun.security.validator.ValidatorException: PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException: unable to find
valid certification path to specified target
我在互联网上进行搜索,遇到了不同的帖子,但这没有帮助。 我使用keytool将证书导入了JRE信任库,但这也无济于事。
HeatlhCheck.java
import java.net.HttpURLConnection;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;
import java.lang.String;
/* This is the maim entry point for the script*/
public class HealthCheck
{
public static void main(String args[])
{
//instancetiate the URLCHEck class
String url = "https://Apache:port/mywebsite";
int timeout = 1000000;
int sitestatus = 0;
URLCheck site = new URLCheck();
sitestatus = site.isURLUp(url,timeout);
if(sitestatus==0)
{
System.out.println("\n Site is up");
}
else if (sitestatus==-1)
{
System.out.println("\n Could not determine site status ");
}
else
{
System.out.println("\n Site is is else condition, down ");
}
}
}
URLCheck.java
import java.net.HttpURLConnection;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;
import java.lang.String;
public class URLCheck
{
public int isURLUp(String url, int timeout)
{
//URLS Checks for checking whether the URL's are accessible or not.
System.out.println("\n URL passed is : "+url);
int responseCode = 3;
System.out.println("\n URL after Trimming : : "+url);
try {
HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
connection.setConnectTimeout(timeout);
connection.setReadTimeout(timeout);
connection.setRequestMethod("HEAD");
responseCode = connection.getResponseCode();
System.out.println("I am Here");
System.out.println("response Code = "+responseCode);
if (responseCode != 200)
{
return 1;
}
}
catch (IOException exception)
{
System.out.println("Exception caught"+exception);
}
return -2;
}
}