The main idea of sending HTTPS requests is to ignore certificate verification.
/***** @ Param URL * @ Param contexttype "image/JPEG", "application/JSON" * @ return */public static byte [] sendhttpsgeturl (httpclient httpclient1, string URL, string contexttype) {// response content byte [] BS = NULL; // create the default httpclient instance // httpclient = new defaulthttpclient (); httpclient = httpclient1; // create trustmanagerx509trustmanager xtm = new x509trustmanager () {public void checkclienttrusted (Login [] Chain, string authtype) throws certificateexception {} public void checkservertrusted (LOGIN [] Chain, string authtype) throws certificateexception {} public x509certificate [] getacceptedissuers () {return New x509certificate [] {}}}; try {sslcontext CTX = sslcontext. getinstance ("SSL"); // use trustmanager to initialize the context. trustmanager is only used by SSL socket CTX. init (null, new trustmanager [] {xtm }, Null); sslsocketfactory Sf = new sslsocketfactory (CTX, sslsocketfactory. allow_all_hostname_verifier); Scheme Sch = new scheme ("HTTPS", 443, SF); httpclient. getconnectionmanager (). getschemeregistry (). register (Sch); // create httpposthttpget httppost = new httpget (URL); httppost. setheader ("Content-Type", contexttype); // execute the POST request httpresponse response = httpclient.exe cute (httppost); // obtain the response entity httpentity enti Ty = response. getentity (); BS = ioutils. tobytearray (entity. getcontent (); If (null! = Entity) {entityutils. consume (entity); // consume response content} return BS;} catch (exception e) {e. printstacktrace ();} finally {// close the connection and release the resource // httpclient. getconnectionmanager (). shutdown ();} return BS ;}
If consecutive requests do not require cookie, you can modify this method. The method parameter does not contain httpclient. In this case, remember to close the connection in the ifinally block.
The following shows Maven required:
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.2.3</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependency>
Httpclient sends HTTPS requests