我正在尝试使用API从Java发送短信。
我只需要使用URL中编写的参数发出https请求即可。
如果我复制生成的URL并将其直接粘贴到我的浏览器中,它将发送SMS。
我在PHP上有使用它的有效代码,但我需要在JAVA上使用它
我已经试过了:
try
{
HttpsURLConnection con;
URL url = new URL(pre_url);
con = (HttpsURLConnection) url.openConnection();
System.out.println(url.getContent());
} catch (MalformedURLException ex) {
Logger.getLogger(crearUsuarioBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(crearUsuarioBean.class.getName()).log(Level.SEVERE, null, ex);
}
但我收到此错误:
Severe: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)...
我还按照一些说明使用keytool将带有.cer的证书添加到cacerts中,但没有成功。我有两个文件夹,jdk1.8.0_131和jre1.8.0_131,因此我已经尝试在两个文件夹上安装证书。仍然没有成功。
这是使我在PHP上工作的代码:
function enviarMensaje($cel, $solicitud, $id){
$mensaje="My message";
$url = "https://notificame.claro.com.hn/api/http/send_to_contact?msisdn=504".$cel."&message=".$mensaje."&api_key=MyAppiKey&id=".$id;
$url = str_replace(" ", "%20", $url);
file($url);
echo $url."<br />\n";
}
我只需要这样简单的东西。我正在使用Primefaces开发JSF2.2
或者也许是直接从Java调用简单php代码的某种方法。
请帮助。
经过几天的相同问题,我终于找到了可能对某些人有用的解决方案。
诀窍是创建一个TrustManager,这样它就可以忽略所有SSL内容。
我添加了以下代码:
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
在此之后,与HTTPS网址建立的任何连接都将起作用。
我在这里找到了解决方案:https : //nakov.com/blog/2009/07/16/disable-certificate-validation-in-java-ssl-connections/
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句