private int conn (String search)throws Exception {
String key="mykey";
URL url = new URL("https://www.googleapis.com/customsearch/v1?q="+search+"&cx=017576662512468239146%3Aomuauf_lfve&key="+key);
doTrustToCertificates();
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setDoOutput(true);
BufferedReader reader = new BufferedReader(new InputStreamReader((connection.getInputStream())));
String splitreader = "";
StringBuilder ss = new StringBuilder();
while((splitreader = reader.readLine()) != null){
ss.append(splitreader);
}
JSONObject json = new JSONObject(ss.toString());
JSONObject jsonrequest = json.getJSONObject("queries").getJSONArray("request").getJSONObject(0);
return jsonrequest.getInt("totalResults");
}
public void doTrustToCertificates() throws Exception {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
return;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
return;
}
}
};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
if (!urlHostName.equalsIgnoreCase(session.getPeerHost())) {
System.out.println("Warning: URL host '" + urlHostName + "' is different to SSLSession host '" + session.getPeerHost() + "'.");
}
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
}
java.lang.nosuchmethoderror:sun.security.ssl.sslsessionimpl.(lsun/security/ssl/protocolversion;lsun/security/ssl/ciphersuite;ljava/util/collection;lsun/security/ssl/sessionid;ljava/lang/string;i)V
但是代码可以在glassfish 4.1.1和wildfly上工作,我不明白为什么不能在glassfish 5.0上工作
检查您的Java版本。这是由于JDK8U161左右的JDK的变化影响了Grizzly。
总结如下: