[英]set client request time out for https ssl connection(mutual authentication)
下面的代码是客户端的示例/片段,用于将json内容发布到HTTPS静态Web服务(在python中开发)。
如何设置客户端发布请求网址(https)的超时(毫秒)。 如何进行重试以多次轮询服务器ulr。 我可以在google和stackoverflow中看到有关http url服务的示例,但对于https(安全连接)则没有。 提前致谢。
import java.io.PrintStream;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.HttpsURLConnection;
public class App{
public static void main(String []args) throws NoSuchAlgorithmException, KeyManagementException{
String https_url = "https://192.168.14.5:8989/books";
String content = "{\"data\":{\"aaa\"}}";
System.setProperty("javax.net.useSystemProxies", "true");
System.setProperty("javax.net.ssl.keyStore", "C:/cert/client_cert.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "testdev");
System.setProperty("javax.net.ssl.trustStore", "C:/cert/server_cert.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "testdev");
System.setProperty("jdk.tls.client.protocals", "TLSv1.2");
System.setProperty("https.protocals", "TLSv1.2");
HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> { return true;});
URL url = new URL(https_url);
HttpsURLConnection httpsCon = (HttpsURLConnection) url.openConnection();
httpsCon.setRequestProperty("Content-Type", "application/json");
httpsCon.setRequestMethod("POST");
httpsCon.setDoOutput(true);
PrintStream ps = new PrintStream(httpsCon.getOutputStream());
ps.println(content);
ps.close();
httpsCon.connect();
httpsCon.getResponseCode();
httpsCon.disconnect();
}
}
您可以使用Apache HttpClient库,通常它由REST easy或AXIS2之类的许多框架用于实现REST和SOAP传输。
例如:
// for the retry use setRetryHandler
CloseableHttpClient httpClient = builder.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(20000).setSoKeepAlive(true).build()).setConnectionTimeToLive(2000, TimeUnit.MILLISECONDS).build();
HttpGet httpGet = new HttpGet("https://mypython.com");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = getHttpClient().execute(httpGet, responseHandler);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.