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

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

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 中工作。

我尝试过这个:

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";   
}

我只需要类似这样简单的东西。我正在使用 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:

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

but Im getting this error:

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)...

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:

function enviarMensaje($cel, $solicitud, $id){     
$mensaje=&quot;My message&quot;;
$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;
$url = str_replace(&quot; &quot;, &quot;%20&quot;, $url);
file($url);
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:

// 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(&quot;SSL&quot;);
    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/

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:

确定