无法找到有效的证书路径到请求的目标,尝试通过 HTTPS 请求使用短信 API。

huangapple go评论156阅读模式
英文:

Unable to find valid certification path to requested target, trying to use a SMS api via https request

问题

我正在尝试使用 Java 发送短信,使用一个 API。

我只需要通过 URL 写入参数并进行 HTTPS 请求。

如果我复制生成的 URL 并直接粘贴到浏览器中,它就会发送短信。

我已经有一个在 PHP 中使用这个的工作代码,但我需要它在 JAVA 中工作。

我尝试过这个:

  1. try
  2. {
  3. HttpsURLConnection con;
  4. URL url = new URL(pre_url);
  5. con = (HttpsURLConnection) url.openConnection();
  6. System.out.println(url.getContent());
  7. }
  8. catch (MalformedURLException ex)
  9. {
  10. Logger.getLogger(crearUsuarioBean.class.getName()).log(Level.SEVERE, null, ex);
  11. }
  12. catch (IOException ex)
  13. {
  14. Logger.getLogger(crearUsuarioBean.class.getName()).log(Level.SEVERE, null, ex);
  15. }

但我遇到了这个错误:

  1. 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
  2. at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
  3. at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
  4. at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)...

我还尝试按照一些说明使用 keytool 将 .cer 证书添加到 cacerts,但没有成功。我有两个文件夹,jdk1.8.0_131 和 jre1.8.0_131,所以我已经尝试在两者上安装证书了。仍然没有成功。

这是在 PHP 上为我工作的代码:

  1. function enviarMensaje($cel, $solicitud, $id){
  2. $mensaje="My message";
  3. $url = "https://notificame.claro.com.hn/api/http/send_to_contact?msisdn=504".$cel."&message=".$mensaje."&api_key=MyAppiKey&id=".$id;
  4. $url = str_replace(" ", "%20", $url);
  5. file($url);
  6. echo $url."<br />\n";
  7. }

我只需要类似这样简单的东西。我正在使用 JSF2.2 并使用 Primefaces。

或者也许有一种直接从 Java 调用简单的 PHP 代码的方法。

请帮忙。

英文:

I'm trying to send a SMS from java using an API.

I just need to make an https request with parameters written in the URL.

https://notificame.claro.com.hn/api/http/send_to_contact?msisdn504999999=&amp;message=mymessage&amp;api_key=00000&amp;id=000

If I copy the resulting url and paste it directly on my browser, it sends the SMS.

I have a working code using this on PHP, but I need it working on JAVA

I've tried this:

  1. try
  2. {
  3. HttpsURLConnection con;
  4. URL url = new URL(pre_url);
  5. con = (HttpsURLConnection) url.openConnection();
  6. System.out.println(url.getContent());
  7. } catch (MalformedURLException ex) {
  8. Logger.getLogger(crearUsuarioBean.class.getName()).log(Level.SEVERE, null, ex);
  9. } catch (IOException ex) {
  10. Logger.getLogger(crearUsuarioBean.class.getName()).log(Level.SEVERE, null, ex);
  11. }

but Im getting this error:

  1. 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
  2. at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
  3. at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
  4. at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)...

I also followed some instructions using keytool to add the certificate with .cer to cacerts but had no success. I have two folders, jdk1.8.0_131 and jre1.8.0_131, so I tried installing the certificate on both already. Still no success.

This is the code that makes the job for me on PHP:

  1. function enviarMensaje($cel, $solicitud, $id){
  2. $mensaje=&quot;My message&quot;;
  3. $url = &quot;https://notificame.claro.com.hn/api/http/send_to_contact?msisdn=504&quot;.$cel.&quot;&amp;message=&quot;.$mensaje.&quot;&amp;api_key=MyAppiKey&amp;id=&quot;.$id;
  4. $url = str_replace(&quot; &quot;, &quot;%20&quot;, $url);
  5. file($url);
  6. echo $url.&quot;&lt;br /&gt;\n&quot;;

}

I just need something simple like this.
Im working on JSF2.2 using Primefaces

Or maybe some way to call a simple php code directly from java.

Help please.

答案1

得分: 0

以下是您要翻译的内容:

"After days with the same problem I finally got a solution that might work for some of you.

The trick is to create a TrustManager, that way it can ignore all the SSL stuff.

I added this code:

// 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);

after this any connection made to an HTTPS url will work.

I found the solution here:
https://nakov.com/blog/2009/07/16/disable-certificate-validation-in-java-ssl-connections/"

请注意,这是您提供的英文文本的中文翻译。

英文:

After days with the same problem I finally got a solution that might work for some of you.

The trick is to create a TrustManager, that way it can ignore all the SSL stuff.

I added this code:

  1. // Create a trust manager that does not validate certificate chains
  2. TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
  3. public java.security.cert.X509Certificate[] getAcceptedIssuers() {
  4. return null;
  5. }
  6. public void checkClientTrusted(X509Certificate[] certs, String authType) {
  7. }
  8. public void checkServerTrusted(X509Certificate[] certs, String authType) {
  9. }
  10. }
  11. };
  12. // Install the all-trusting trust manager
  13. SSLContext sc = SSLContext.getInstance(&quot;SSL&quot;);
  14. sc.init(null, trustAllCerts, new java.security.SecureRandom());
  15. HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
  16. // Create all-trusting host name verifier
  17. HostnameVerifier allHostsValid = new HostnameVerifier() {
  18. public boolean verify(String hostname, SSLSession session) {
  19. return true;
  20. }
  21. };
  22. // Install the all-trusting host verifier
  23. HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

after this any connection made to an HTTPS url will work.

I found the solution here:
https://nakov.com/blog/2009/07/16/disable-certificate-validation-in-java-ssl-connections/

huangapple
  • 本文由 发表于 2020年8月5日 04:13:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63254421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定