`
fengfangfang
  • 浏览: 25598 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

SSL访问

    博客分类:
  • java
阅读更多
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();
        }
    }
}

 

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics