尝试通过https get' javax.net.ssl.sslexception发送数据时:接收致命警报:internal_error'
我正在尝试使用API发送HTTPS REQ。我在尝试从连接中获取todutputstream时使用了这种剪裁并遇到错误。在此功能中,首先我创建了一个连接,然后尝试使用任何HTTP/HTTPS进行连接。然后只需使用Connection.getOutputStream将响应解析为数组。此代码仅适用于HTTP。
public String sendNativePost(String httpURL, Map headers, String body, String respType)
throws Exception {
URL url = new URL(httpURL);
HttpURLConnection connection;
if (url.getProtocol().toLowerCase().equals("https")) {
url = new URL(null, httpURL, new sun.net.www.protocol.https.Handler());
trustAllHosts();
HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
https.setHostnameVerifier(DO_NOT_VERIFY);
connection = https;
} else {
connection = (HttpURLConnection) url.openConnection();
}
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "*/*");
if (headers != null) {
for (Object key : headers.keySet()) {
connection.setRequestProperty((String) key, (String) headers.get(key));
}
} else {
logger.error("Header is Empty");
}
connection.setConnectTimeout(10 * 1000);
connection.setReadTimeout(10 * 1000);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty(
"User-Agent", SMSServer.APPLICATION_NAME + "_" + SMSServer.APPLICATION_VERSION);
try (OutputStream os = connection.getOutputStream()) {
byte[] input = body.getBytes(StandardCharsets.UTF_8);
logger.debug("get body-");
os.write(input, 0, input.length);
logger.debug("write body");
} catch (Exception e) {
logger.error("Output Stream Error!", e);
}
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String responseMessage = "";
while (true) {
String line = br.readLine();
if (line != null) {
responseMessage += line;
} else {
break;
}
}
br.close();
connection.disconnect();
return responseMessage;
}
并收到以下错误:
2022-06-13 17:17:41:323 ERROR HttpClient:224 - Output Stream Error!
javax.net.ssl.SSLException: Received fatal alert: internal_error
at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:2023)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1125)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1316)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1291)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
at smsclient.HTTPClient.sendNativePost(HTTPClient.java:217)
at smsclient.SMSClientHttp.sendPOST(SMSClientHttp.java:256)
at smsclient.SMSClientHttp.sendSMS(SMSClientHttp.java:147)
at smsclient.SMSClientHttp.processSMS(SMSClientHttp.java:42)
at smsclient.HTTPClientMessageSender.run(HTTPClientMessageSender.java:75)
如果您需要更多信息,请告诉我
I am trying to send https req using an API. I have used this sniped of code and getting error while trying to getOutputStream from the connection. In this function, first i have created a connection then trying to connect using either http/https. then just parse the response into an array using connection.getOutputStream. This code working fine for http only.
public String sendNativePost(String httpURL, Map headers, String body, String respType)
throws Exception {
URL url = new URL(httpURL);
HttpURLConnection connection;
if (url.getProtocol().toLowerCase().equals("https")) {
url = new URL(null, httpURL, new sun.net.www.protocol.https.Handler());
trustAllHosts();
HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
https.setHostnameVerifier(DO_NOT_VERIFY);
connection = https;
} else {
connection = (HttpURLConnection) url.openConnection();
}
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "*/*");
if (headers != null) {
for (Object key : headers.keySet()) {
connection.setRequestProperty((String) key, (String) headers.get(key));
}
} else {
logger.error("Header is Empty");
}
connection.setConnectTimeout(10 * 1000);
connection.setReadTimeout(10 * 1000);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty(
"User-Agent", SMSServer.APPLICATION_NAME + "_" + SMSServer.APPLICATION_VERSION);
try (OutputStream os = connection.getOutputStream()) {
byte[] input = body.getBytes(StandardCharsets.UTF_8);
logger.debug("get body-");
os.write(input, 0, input.length);
logger.debug("write body");
} catch (Exception e) {
logger.error("Output Stream Error!", e);
}
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String responseMessage = "";
while (true) {
String line = br.readLine();
if (line != null) {
responseMessage += line;
} else {
break;
}
}
br.close();
connection.disconnect();
return responseMessage;
}
And got the following error:
2022-06-13 17:17:41:323 ERROR HttpClient:224 - Output Stream Error!
javax.net.ssl.SSLException: Received fatal alert: internal_error
at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:2023)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1125)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1316)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1291)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
at smsclient.HTTPClient.sendNativePost(HTTPClient.java:217)
at smsclient.SMSClientHttp.sendPOST(SMSClientHttp.java:256)
at smsclient.SMSClientHttp.sendSMS(SMSClientHttp.java:147)
at smsclient.SMSClientHttp.processSMS(SMSClientHttp.java:42)
at smsclient.HTTPClientMessageSender.run(HTTPClientMessageSender.java:75)
Please let me know if you need anymore info
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论