Java calls the HTTPS interface, avoids the way the certificate

Source: Internet
Author: User

One, call with HttpClient

1. Add dependencies using Maven

<dependency>            <groupId>org.apache.httpcomponents</groupId>            <artifactId> httpclient</artifactid>            <version>4.5.  3</version>        </dependency>        <dependency>            <groupId> org.apache.httpcomponents</groupid>            <artifactId>httpmime</artifactId>            <version >4.5. 3</version>        </dependency>
View Code

2. Testing

Package Com.moy.whymoy.test;import Org.apache.http.httpentity;import Org.apache.http.client.methods.closeablehttpresponse;import Org.apache.http.client.methods.httpget;import Org.apache.http.client.methods.httppost;import Org.apache.http.conn.ssl.noophostnameverifier;import Org.apache.http.conn.ssl.sslconnectionsocketfactory;import Org.apache.http.conn.ssl.truststrategy;import Org.apache.http.entity.mime.multipartentitybuilder;import org.apache.http.impl.client.CloseableHttpClient; Import Org.apache.http.impl.client.httpclients;import Org.apache.http.ssl.sslcontexts;import Org.apache.http.util.entityutils;import Javax.net.ssl.*; import java.io.*; import Java.net.url;import java.security.keymanagementexception;import java.security.KeyStoreException; Import Java.security.nosuchalgorithmexception;import Java.security.cert.certificateexception;import Java.security.cert.X509Certificate;/** * [Project]:whymoy <br/> * [email]:[email protected] <br/> * [DATE]:2018/3/14 <br/> * [descript ION]: <br/> * * @author Yexiangyang*/ Public classMain { Public Static voidMain (string[] args) throws IOException, NoSuchAlgorithmException, Keystoreexception, keymanagementexception { String URL="https://kyfw.12306.cn/otn/"; Try(Closeablehttpclient httpClient =createhttpclient ()) {HttpGet HttpGet=Newhttpget (URL); Try(Closeablehttpresponse HttpResponse =Httpclient.execute (HttpGet)) {httpentity entity=httpresponse.getentity (); String result=entityutils.tostring (entity);                                Entityutils.consume (entity); System. out. printf (result); }        }    }    Private Staticcloseablehttpclient createhttpclient () throws Keystoreexception, NoSuchAlgorithmException, keymanagementexception {sslcontext Sslcontext=Sslcontexts.custom (). Loadtrustmaterial (NULL, (chain, AuthType)true). build (); Sslconnectionsocketfactory SSLSF=NewSslconnectionsocketfactory (Sslcontext,NULL,NULL,                Newnoophostnameverifier ()); returnHttpclients.custom (). Setsslsocketfactory (SSLSF). build (); }}
View Code

Second, using native httpsurlconnection call

Package Com.moy.whymoy.test;import Javax.net.ssl.httpsurlconnection;import javax.net.ssl.sslcontext;import Javax.net.ssl.trustmanager;import Javax.net.ssl.x509trustmanager;import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.net.url;import Java.security.keymanagementexception;import Java.security.nosuchalgorithmexception;import Java.security.cert.certificateexception;import java.security.cert.X509Certificate;/** * [Project]:whymoy <br/> * [email]:[email protected] <br/> * [DATE]:2018/3/27 <br/> * [descript ION]: <br/> * * @author Yexiangyang*/ Public classRun { Public Static voidMain (string[] args) throws Keymanagementexception, NoSuchAlgorithmException, ioexception {String URL="https://kyfw.12306.cn/otn/"; Sslcontext SC=Createsslcontext (); Httpsurlconnection Conn= (httpsurlconnection)Newurl (url). OpenConnection ();        Conn.setsslsocketfactory (Sc.getsocketfactory ()); Conn.sethostnameverifier (S, sslsession)-true); Conn.setdoinput (true); Conn.setdooutput (true);        Conn.connect (); StringBuilder result=NewStringBuilder (); Try(BufferedReader br =NewBufferedReader (NewInputStreamReader (Conn.getinputstream ())))            {String line;  while(NULL! = (line =Br.readline ())) {Result.append (line). Append ("\ n");        }} conn.disconnect (); System. out. printf (result.tostring ()); }    Private StaticSslcontext Createsslcontext () throws NoSuchAlgorithmException, keymanagementexception {sslcontext sc= Sslcontext.getinstance ("SSL"); Sc.init (NULL,Newtrustmanager[]{NewX509trustmanager () {@Override Public voidcheckclienttrusted (x509certificate[] x509certificates, String s) throws certificateexception {} @Override Public voidcheckservertrusted (x509certificate[] x509certificates, String s) throws certificateexception {} @Override Publicx509certificate[] Getacceptedissuers () {return Newx509certificate[0]; }        }}, Newjava.security.SecureRandom ()); returnSC; }}
View Code

Yexiangyang

[Email protected]

Java calls the HTTPS interface, avoids the way the certificate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.