简体   繁体   English

设置https ssl连接的客户端请求超时(相互认证)

[英]set client request time out for https ssl connection(mutual authentication)

Below code is sample/ snippet of client to post json content to HTTPS restful web service(developed in python). 下面的代码是客户端的示例/片段,用于将json内容发布到HTTPS静态Web服务(在python中开发)。

How can I set the time out(millseconds) for the client post request url(https). 如何设置客户端发布请求网址(https)的超时(毫秒)。 How I can do the retry to poll the server ulr for multiple times. 如何进行重试以多次轮询服务器ulr。 I can see the examples for http url service in google and stackoverflow but no for the https(secure connection). 我可以在google和stackoverflow中看到有关http url服务的示例,但对于https(安全连接)则没有。 thanks in advance. 提前致谢。

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();
     }
}

You can use Apache HttpClient library, usually it is used for implementing REST and SOAP transport by many framework like REST easy or AXIS2. 您可以使用Apache HttpClient库,通常它由REST easy或AXIS2之类的许多框架用于实现REST和SOAP传输。

For example: 例如:

 // 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM