我有一个Java实现,被各种客户端应用程序用于连接到第三方系统。这些第三方系统支持通过http / https的不同协议。在这种情况下,所有客户端应用程序都托管在我的Java实现托管的同一台服务器上。因此,在这种情况下,各种客户端应用程序将各种https协议设置为系统属性(例如:System.setProperty("https.protocols", "SSLv3")
,System.setProperty("https.protocols", "TLS")
),当他们使用它来连接到那些第三方系统时。
在这里,系统属性在该环境中共享。因此,修改系统属性会导致许多问题。所以,我想知道:
- 是否有任何方法可以在不使用系统属性的情况下完成此操作?
- 是否有任何方法可以设置所有可能的https.protocols,以便它支持对第三方系统支持的各种协议进行的任何http或https连接?
每个JDK版本支持的协议和算法如blogs.oracle.com所述:
![enter image description here](https://istack.dev59.com/0Th5x.webp)
代码:
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