Java中請求HTTPS加密的原始碼

來源:互聯網
上載者:User

 HTTPS存取方法的代碼,抄一個能用的DEMO吧,我測試過的,可以用、,希望對大家也有用

 

import java.io.*;
import java.net.*;
import java.security.*;
import java.security.cert.*;
import java.util.*;
import javax.net.ssl.*;

public class HttpsTest ...{
    // We would never hardcode this literal in a real system,
    // this is only for this article.
    private String url = "https://www.paypal.com/cn";

    // Create an anonymous class to trust all certificates.
    // This is bad style, you should create a separate class.
    private X509TrustManager xtm = new X509TrustManager() ...{
        public void checkClientTrusted(X509Certificate[] chain, String authType) ...{}

            public void checkServerTrusted(X509Certificate[] chain, String authType) ...{
                System.out.println("cert: " + chain[0].toString() + ", authType: " + authType);
            }

            public X509Certificate[] getAcceptedIssuers() ...{
                return null;
            }
    }; 

    // Create an class to trust all hosts
    private HostnameVerifier hnv = new HostnameVerifier() ...{
        public boolean verify(String hostname, SSLSession session) ...{
            System.out.println("hostname: " + hostname);
            return true;
        }
    }; 

    // In this function we configure our system with a less stringent
    // hostname verifier and X509 trust manager.  This code is
    // executed once, and calls the static methods of HttpsURLConnection
    public HttpsTest() ...{
        // Initialize the TLS SSLContext with
        // our TrustManager
        SSLContext sslContext = null;

        try ...{
            sslContext = SSLContext.getInstance("TLS");
            X509TrustManager[] xtmArray = new X509TrustManager[] ...{ xtm };
            sslContext.init(null, xtmArray, new java.security.SecureRandom());
        } catch(GeneralSecurityException gse) ...{
            // Print out some error message and deal with this exception
        }

        // Set the default SocketFactory and HostnameVerifier
        // for javax.net.ssl.HttpsURLConnection
        if(sslContext != null) ...{
            HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
        }

        HttpsURLConnection.setDefaultHostnameVerifier(hnv);
    }

    // This function is called periodically, the important thing
    // to note here is that there is no special code that needs to
    // be added to deal with a "HTTPS" URL.  All of the trust
    // management, verification, is handled by the HttpsURLConnection.
    public void run() ...{
        try ...{
            URLConnection urlCon = (new URL(url)).openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
            String line;

            while((line = in.readLine()) != null) ...{
                System.out.println(line);
            }

        //  Whatever we want to do with these quotes
        } catch(MalformedURLException mue) ...{
            mue.printStackTrace();
        } catch(IOException ioe) ...{
            ioe.printStackTrace();
        } catch(Exception e) ...{
            e.printStackTrace();
        }
    }

    public static void main(String[] args) ...{
        HttpsTest httpsTest = new HttpsTest();
        httpsTest.run();
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.