Package Com.lichmama.test.util;import Java.io.bytearrayoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.io.outputstream;import Java.net.url;import java.security.KeyManagementException; Import Java.security.nosuchalgorithmexception;import Java.security.securerandom;import Java.security.cert.certificateexception;import Java.security.cert.x509certificate;import Javax.net.ssl.hostnameverifier;import Javax.net.ssl.httpsurlconnection;import Javax.net.ssl.KeyManager;import Javax.net.ssl.sslcontext;import Javax.net.ssl.sslsession;import Javax.net.ssl.sslsocketfactory;import Javax.net.ssl.trustmanager;import Javax.net.ssl.x509trustmanager;public class Httpsutil {private static final class De Faulttrustmanager implements X509trustmanager {@Override public void checkclienttrusted (x509certificate[] C Hain, String authtype) throws certificateexception {} @Override public void checkservertrusted (x509c Ertificate[] chain, String authtype) throws Certificateexception {} @Override public x509certificate[] Getacceptedissuers () { return null; }} private static Httpsurlconnection gethttpsurlconnection (string uri, String method) throws IOException {S Slcontext CTX = null; try {ctx = sslcontext.getinstance ("TLS"); Ctx.init (New keymanager[0], new trustmanager[] {new Defaulttrustmanager ()}, New SecureRandom ()); } catch (Keymanagementexception e) {e.printstacktrace (); } catch (NoSuchAlgorithmException e) {e.printstacktrace (); } sslsocketfactory SSF = Ctx.getsocketfactory (); URL url = new URL (URI); Httpsurlconnection httpsconn = (httpsurlconnection) url.openconnection (); Httpsconn.setsslsocketfactory (SSF); Httpsconn.sethostnameverifier (New Hostnameverifier () {@Override public boolean verify (String arg0, SSLsession arg1) {RETUrn true; } }); Httpsconn.setrequestmethod (method); Httpsconn.setdoinput (TRUE); Httpsconn.setdooutput (TRUE); return httpsconn; } private static byte[] Getbytesfromstream (InputStream is) throws IOException {Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); Byte[] kb = new byte[1024]; int Len; while (len = Is.read (KB))! =-1) {Baos.write (KB, 0, Len); } byte[] bytes = Baos.tobytearray (); Baos.close (); Is.close (); return bytes; } private static void Setbytestostream (OutputStream os, byte[] bytes) throws IOException {Bytearrayinputstream Bais = new Bytearrayinputstream (bytes); Byte[] kb = new byte[1024]; int Len; while (len = Bais.read (KB))! =-1) {Os.write (KB, 0, Len); } os.flush (); Os.close (); Bais.close (); } public static byte[] Doget (String uri) throws IOException { Httpsurlconnection httpsconn = gethttpsurlconnection (URI, "GET"); Return Getbytesfromstream (Httpsconn.getinputstream ()); } public static byte[] DoPost (string uri, String data) throws IOException {httpsurlconnection Httpsconn = getht Tpsurlconnection (URI, "POST"); Setbytestostream (Httpsconn.getoutputstream (), data.getbytes ()); Return Getbytesfromstream (Httpsconn.getinputstream ()); }}
Download a file (Bing today) under test:
public class Testhttps {public static void Main (string[] args) throws IOException { String uri = "https://cn.bing . com/hpwp/356677bea977a9aa166a92ab94848f17 "; byte[] bytes = Httpsutil.doget (URI); FileOutputStream fos = new FileOutputStream ("d:/bing.picture-of-day.jpg"); Fos.write (bytes); Fos.close (); System.out.println ("done!");} }
When using the above code in WebLogic, ClassCastException may appear, details and solutions to see the following links:
http://www.th7.cn/program/java/201511/688262.shtmlhttp://blog.csdn.net/arvinrong/article/details/7715334[
[GO] Java implementation HTTPS request