【发布时间】:2016-03-20 22:12:16
【问题描述】:
我有一个 Java 实现,各种客户端应用程序使用它来连接到第三方系统。这些第三方系统通过 http/https 支持不同的协议。在这种情况下,所有客户端应用程序都托管在我的 Java 实现托管的同一台服务器中。因此,在这种情况下,各种客户端应用程序将各种 https 协议设置为系统属性(例如:System.setProperty("https.protocols", "SSLv3"),System.setProperty("https.protocols", "TLS"),当他们使用它连接到那些第三方系统时。
在这里,系统属性在该环境中的所有应用程序之间共享。因此,修改系统属性会导致很多问题。所以,我想知道,
- 有没有办法在不使用系统属性的情况下完成这项工作?
- 有没有办法设置所有可能的 https.protocols 以便它 支持与第三方建立的任何 http 或 https 连接 系统支持各种协议?
blogs.oracle.com 中提到的每个 JDK 版本支持的协议和算法:
代码:
String responseStr = null;
System.setProperty("https.protocols",http-protocol); // This is set by the client applications. Previously, there was one by one (eg : "SSLv3". Then I changed it to "TLSv1.2,TLSv1.1,TLSv1,SSLv3" assuming it will enable all)
byteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byteArrayOutputStream.write(requestStr.getBytes());
URL mUrl = new URL(proxy_url);
HttpURLConnection con = (HttpURLConnection) mUrl.openConnection(); // It works fine for the HttpURLConnection when there's no (s)
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setUseCaches(false);
con.setDoInput(true);
con.setRequestProperty("user-agent","Mozilla(MSIE)");
con.setRequestProperty("Accept-Encoding","gzip,deflate");
byteArrayOutputStream.writeTo(con.getOutputStream());
String encodingHeader = con.getHeaderField("Content-Encoding");
InputStream inputStream = null;
if(encodingHeader != null && encodingHeader.toLowerCase().indexOf("gzip") != -1){
inputStream = new GZIPInputStream(con.getInputStream());
}else {
inputStream = con.getInputStream();
}
if (inputStream != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int length = 0;
while ((length = inputStream.read(buffer)) != -1) {
baos.write(buffer, 0, length);
}
responseStr = new String(baos.toByteArray());
baos.close();
}
我的 Java 版本:1.5
【问题讨论】:
-
您需要升级。 1.5 十年前问世。
-
确实如此。但是现在有一个很大的解决方法。所以,我正在寻找当前版本的解决方案
-
这个问题的答案可能是相关的:superuser.com/questions/747377/… 值得注意的是 Oracle 文档,它显示虽然 Java 1.7 支持 TLS1.2,但默认情况下它对客户端连接是禁用的。
-
ByteArrayOutputStream 在这里浪费时间和空间。