java加載證書,訪問12306的https鏈接

下載12306的證書,訪問12306網站,下載12306的證書

1111.jpg

導出證書

2222.jpg

3333.jpg

導出完成後,導入證書

4444.jpg

查看是否導入

5555.jpg

java中加載證書

public static void main( String[] args ){
	PrintWriter out = null;
    BufferedReader in = null;
    String result = "";
    //設置證書
    System.setProperty("javax.net.ssl.trustStore","D:\\Program Files\\Java\\jdk1.7.0_79\\bin\\12306d.keystore");  
    System.setProperty("javax.net.ssl.trustStorePassword", "12306java");  
    try {
        URL realUrl = new URL("https://kyfw.12306.cn/otn/login/loginAysnSuggest");
        // 打開和URL之間的連接
        URLConnection conn = realUrl.openConnection();
        // 設置通用的請求屬性
        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
        conn.setRequestProperty("user-agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        // 發送POST請求必須設置如下兩行
        conn.setDoOutput(true);
        conn.setDoInput(true);
        // 獲取URLConnection對象對應的輸出流
        out = new PrintWriter(conn.getOutputStream());
        // 發送請求參數
        out.print("loginUserDTO.user_name=aaa&userDTO.password=123456&randCode=182,51,19,130");
        // flush輸出流的緩衝
        out.flush();
        // 定義BufferedReader輸入流來讀取URL的響應
        in = new BufferedReader(
                new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            result += line;
        }
        System.out.println(result);
    } catch (Exception e) {
        System.out.println("發送 POST 請求出現異常!"+e);
        e.printStackTrace();
    }
    //使用finally塊來關閉輸出流、輸入流
    finally{
        try{
            if(out!=null){
                out.close();
            }
            if(in!=null){
                in.close();
            }
        }
        catch(IOException ex){
            ex.printStackTrace();
        }
    }
}


本文由微兒博客原創,博客地址:www.weare.net.cn 或者點擊 微兒博客

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章