Realization of SSL landing

zhaozj2021-02-16  130

The main program is as follows:

public static void main (String strArgv []) throws Exception {X509TrustManager xtm = new MyTrustManager (); TrustManager mytm [] = {xtm}; SSLContext ctx = SSLContext.getInstance ( "SSL"); ctx.init (null, mytm, NULL); sslsocketfactory sf = ctx.getsocketFactory ();

URL url = new URL ( "yourhttpssite"); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection (); connection.setSSLSocketFactory (sf); connection.setHostnameVerifier (new MyVerified ()); connection.setDoOutput (true); BufferedReader in = new BufferedReader (new InputStreamReader (connection.getInputStream ())); // BufferedReader in // = new BufferedReader (new InputStreamReader (url.openStream ())); String line; while ((line = in.readLine ())! = null) {system.out.println (line);} in.close ();

------------------------- MyTrustManager.java ----------------------- -------------

Package com.netutel.ubisp.util; import javax.net.ssl. *; import java.Net. *; import java.security.cert. *; import java.security.cert.certificateException;

Public Class MyTrustManager Implements X509TrustManager {MyTrustManager () {// Constructor // Create / Load KeyStore}

Public void checkclienttrusted (x509certificate chain ", string authtype) throws certificateException {}

public void checkServerTrusted (X509Certificate chain [], String authType) throws CertificateException {// special handling such as poping dialog boxes} public X509Certificate [] getAcceptedIssuers () {return null;}}

----------------------------- myverified.java -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------- Package com.netutel.ubisp.util; import java.net. *; Import java.ior. *; import java.util. *; import java.io *;. import java.net *;. import javax.net.ssl ​​*;. public class MyVerified implements HostnameVerifier {public boolean verify (String hostname, SSLSession session) {return true;}}

Key in the last class hostnameverifier

Specific articles can look up the network's article

转载请注明原文地址:https://www.9cbs.com/read-7862.html

New Post(0)