SSL访问

import java.net.*;
import javax.net.ssl.*;
import java.io.*;
public class SSLIXBackup111 {
    /** Creates a new instance of SSLIX */
    public SSLIXBackup111() {
    }
    public void connect(String url1,String data) throws Exception {
        URL url = new URL(url1);
        System.setProperty("javax.net.ssl.keyStore", "E:\\test\\clientKeystore.jks");        
        System.setProperty("javax.net.ssl.keyStorePassword", "ChinaMobile");
        System.setProperty("javax.net.ssl.keyStoreType", "jks");
        System.setProperty("javax.net.ssl.trustStorePassword","ChinaMobile");
        System.setProperty("javax.net.ssl.trustStore","E:\\test\\clientTruststore.jks");        

        HostnameVerifier hv = new HostnameVerifier() {
                  public boolean verify(String urlHostName, SSLSession session) {
                           //System.out.println("Warning: URL Host: " + urlHostName  + " vs. " + session.getPeerHost());
                           return true;
                  }
        };
        HttpsURLConnection.setDefaultHostnameVerifier(hv);
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        connection.setRequestProperty("Content-Type", "text/xml");
        connection.setDoOutput(true); // true for POST, false for GET
        connection.setDoInput(true);
        connection.setRequestMethod("POST");
        connection.setUseCaches(false);
        
        StringBuilder content = new StringBuilder();
        content.append(data);

        // send the POST request to server
        OutputStream outputStream = null;
        try {
            outputStream = connection.getOutputStream();
            outputStream.write(content.toString().getBytes("utf-8"));
            outputStream.flush();
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }
        }

        String aLine = null;
        InputStream is = connection.getInputStream();

        File result = new File("result.xml");
        FileWriter fos = new FileWriter(result);
        InputStreamReader inReader = new InputStreamReader(is, "UTF-8");
        BufferedReader aReader = new BufferedReader(inReader);
        //System.err.println("<-------------------------Output------------------------->");
        while ((aLine = aReader.readLine()) != null){
            System.out.println(aLine);
            //fos.write(aLine+"\n");
        }
        fos.close();
        
        inReader.close();
        aReader.close();
        connection.disconnect();
    }
    /**
     * 
     * @param args
     * 
     * the command line arguments
     * 
     */
    public static void main(String[] args) {
        // if (args.length != 2)
        //
        // {
        //
        // System.out.println("USAGE: PingAnCRL url datafile");
        //
        // }
        try {
            SSLIXBackup111 ix = new SSLIXBackup111();
            // System.out.println("URL :" + args[0]);
            // System.out.println("dataFile :" + args[1]);
            String s = "<...>"; 
            String url = "https://192.168.1.38:8443/ResourceSync";
            ix.connect(url,s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值