尝试通过https get' javax.net.ssl.sslexception发送数据时:接收致命警报:internal_error'

发布于 2025-02-07 16:58:48 字数 3389 浏览 1 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文