发新话题
打印

jdk1.6 支持 tls1.2协议的操作过程

jdk1.6 支持 tls1.2协议的操作过程

事件:
因为调用的接口要升级到 tls1.2协议,但是我的项目环境是JDK1.6,功能多,代码乱不敢升级到JDK1.8.

首先下载:commons-httpclient-3.1.jar、bcprov-jdk15on-1.60.jar,放于项目WEB-INFO/lib/。
新建tls目录,将java文件拷贝到这里。
应用案例:
   public static void main(String[] args) {
    HttpsURLConnection  con = null;   
        BufferedReader in = null; 
        //ResultStaffSet resultSet= null;
        System.setProperty("javax.net.debug", "all");
        try {
        String result = "";
        String url = "https://us.zhcw.com/api-user/userbaseinfo/findUserInfoById";
        String param = "";
    TLS12ProtocolSocketFactory tls12ProtocolSocketFactory = new TLS12ProtocolSocketFactory();
    Protocol.registerProtocol("https", new Protocol("https", tls12ProtocolSocketFactory, 443));
    HttpClient httpClient = new HttpClient();
    httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
    System.out.println(url);
    PostMethod httpMethod = new PostMethod(url);
    httpMethod.addRequestHeader("accept", "*/*");
    httpMethod.addRequestHeader("connection", "Keep-Alive");
    httpMethod.addRequestHeader("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
    httpMethod.addRequestHeader("Accept-Charset", "UTF-8");
    httpMethod.addRequestHeader("Content-Type", "application/json");
   
    HttpMethodParams httpParams = new HttpMethodParams();

    httpMethod.setRequestBody(param);

    httpMethod.setParams(httpParams);
    try {
    httpClient.executeMethod(httpMethod);
    result = httpMethod.getResponseBodyAsString();
    System.out.println(result);
    } catch (IOException e) {
    e.printStackTrace();
    }

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

TOP

发新话题