Don't want to say anything, on the code!
PackageCom.my.https;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.OutputStream;Importjava.net.HttpURLConnection;ImportJava.net.URL;ImportJava.security.SecureRandom;ImportJavax.net.ssl.HostnameVerifier;Importjavax.net.ssl.HttpsURLConnection;ImportJavax.net.ssl.KeyManager;ImportJavax.net.ssl.SSLContext;Importjavax.net.ssl.SSLSession;Importjavax.net.ssl.SSLSocketFactory;ImportJavax.net.ssl.TrustManager; Public classHttpurlconnectionfactory { Public Static intDefault_conn_timeour = 30000; Public StaticHttpURLConnection getconn (String URL)throwsThrowable {HttpURLConnection conn=NULL; URL http=Newurl (URL); if(Url.startswith ("https:") {httpsurlconnection Httpsconn=NULL; Sslcontext Sslcontext= Sslcontext.getinstance ("TLS"); Sslcontext.init (NewKeymanager[0],NewTrustmanager[] {NewMyx509trustmanager ()},Newsecurerandom ()); Sslsocketfactory SSF=sslcontext.getsocketfactory (); Httpsconn=(httpsurlconnection) http.openconnection (); Httpsconn.setsslsocketfactory (SSF); Httpsconn.sethostnameverifier (NewHostnameverifier () {@Override Public BooleanVerify (String hostname, sslsession session) {return true; } }); Conn=Httpsconn; } Else{conn=(HttpURLConnection) http.openconnection (); } returnConn; } Public StaticString sendget (httpurlconnection con)throwsthrowable {Addconnprop (con,"GET",true); BufferedReader BR=NULL; StringBuffer Resultbuffer=NewStringBuffer (); Try{br=NewBufferedReader (NewInputStreamReader (Con.getinputstream (), "Utf-8")); String temp; while(temp = Br.readline ())! =NULL) {resultbuffer.append (temp); } } finally { if(BR! =NULL) { Try{br.close (); } Catch(IOException e) {br=NULL; Throw NewRuntimeException (e); } finally { if(Con! =NULL) {con.disconnect (); Con=NULL; } } } } returnresultbuffer.tostring (); } Public StaticString Sendpost (httpurlconnection con, string body)throwsthrowable {Addconnprop (con,"POST",true); OutputStream OutStream=NULL; BufferedReader Responsereader=NULL; StringBuffer SB=NewStringBuffer (); if(Body! =NULL) {OutStream=Con.getoutputstream (); byte[] data =body.getbytes (); Outstream.write (data); Outstream.flush (); Outstream.close (); } intResultCode =Con.getresponsecode (); if(HTTPURLCONNECTION.HTTP_OK = =ResultCode) {String ReadLine=NewString (); Responsereader=NewBufferedReader (NewInputStreamReader (Con.getinputstream (), "UTF-8")); while((ReadLine = Responsereader.readline ())! =NULL) {sb.append (ReadLine). Append ("\ n"); } responsereader.close (); } returnsb.tostring (); } Private Static voidAddconnprop (HttpURLConnection conn, String method,BooleanFlagthrowsThrowable {Conn.setrequestmethod (method); Conn.setconnecttimeout (Default_conn_timeour); Conn.setrequestproperty ("Accept", "*/*"); //conn.setrequestproperty ("Content-type",//"application/x-www-form-urlencoded");Conn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); Conn.setusecaches (false); if(flag) {Conn.setdoinput (true); Conn.setdooutput (true); Conn.setrequestproperty ("Connection", "keep-alive"); } }}
PackageCom.my.https;Importjava.security.cert.CertificateException;Importjava.security.cert.X509Certificate;ImportJavax.net.ssl.X509TrustManager; Public classMyx509trustmanagerImplementsx509trustmanager{@Override Public voidCheckclienttrusted (x509certificate[] arg0, String arg1)throwscertificateexception {} @Override Public voidCheckservertrusted (x509certificate[] arg0, String arg1)throwscertificateexception {} @Override Publicx509certificate[] Getacceptedissuers () {return NULL; }}Myx509trustmanager.java
Still want to say, not measured, don't know right
JDK Native HttpURLConnection Request instance