今天用Request创建一个post API请求, 然后在本地测试是OK的, 但布署到正式环境就报了下面的异常:
Exception in thread "main" org.nutz.http.HttpException: url=https://xxxxxxxxxxxx
at org.nutz.http.sender.PostSender.send(PostSender.java:38)
at com.touyun.crontabjob.hdmilk.util.SyncDataFromPlatform.getData(SyncDataFromPlatform.java:90)
at com.touyun.crontabjob.hdmilk.util.SyncDataFromPlatform.loadBabyInfo(SyncDataFromPlatform.java:51)
at com.touyun.crontabjob.hdmilk.MainSetup.main(MainSetup.java:28)
Caused by: javax.net.ssl.SSLKeyException: RSA premaster secret error
at sun.security.ssl.RSAClientKeyExchange.<init>(RSAClientKeyExchange.java:86)
at sun.security.ssl.ClientHandshaker.serverHelloDone(ClientHandshaker.java:880)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:344)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:936)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:871)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1043)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1343)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1371)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1281)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1256)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
at org.nutz.http.sender.PostSender.send(PostSender.java:29)
... 3 more
Caused by: java.security.NoSuchAlgorithmException: SunTls12RsaPremasterSecret KeyGenerator not available
at javax.crypto.KeyGenerator.<init>(KeyGenerator.java:158)
at javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:207)
at sun.security.ssl.JsseJce.getKeyGenerator(JsseJce.java:272)
at sun.security.ssl.RSAClientKeyExchange.<init>(RSAClientKeyExchange.java:77)
... 17 more
初步认定是验证证书的问题, 为此增加了下面的代码, 关闭检验:
Http.disableJvmHttpsCheck();
但最终的结果还是报同样的错误, 求指教. 源代码如下:
String getData(String url, boolean isNew, String lastMD) {
Map<String, String> headers = new HashMap<String, String>();
headers.put("AuthInfo", "sao.so");
headers.put("Content-Type", "application/json");
String result = "";
String body = "{\"isNew\":" + isNew + ",\"lastMD\":\"" + lastMD + "\"}";
try {
Http.disableJvmHttpsCheck();
Request req = Request.create(url, METHOD.POST);
req.setEnc("UTF-8");
req.setData(body);
req.setHeader(Header.create(headers));
Response resp = Sender.create(req, 5 * 1000).send();
if (resp.isOK()) {
String content = resp.getContent();
return content;
}
return result;
} catch (HttpException e) {
throw Lang.wrapThrow(e, "An exception was happend when get data from paltform.");
}
}