HTTPS Request Tool Class

Source: Internet
Author: User

Import java.io.IOException;
Import java.io.UnsupportedEncodingException;
Import java.lang.reflect.Field;
Import java.security.KeyManagementException;
Import java.security.NoSuchAlgorithmException;
Import java.security.cert.CertificateException;
Import java.security.cert.X509Certificate;
Import java.util.ArrayList;
Import java.util.HashMap;
Import java.util.List;
Import java.util.Map;

Import javax.net.ssl.SSLContext;
Import javax.net.ssl.SSLException;
Import javax.net.ssl.SSLSession;
Import javax.net.ssl.SSLSocket;
Import javax.net.ssl.TrustManager;
Import javax.net.ssl.X509TrustManager;

Import org.apache.http.HttpEntity;
Import org.apache.http.HttpResponse;
Import org.apache.http.NameValuePair;
Import org.apache.http.ParseException;
Import org.apache.http.client.ClientProtocolException;
Import org.apache.http.client.HttpClient;
Import org.apache.http.client.entity.UrlEncodedFormEntity;
Import org.apache.http.client.methods.HttpPost;
Import org.apache.http.conn.scheme.Scheme;
Import org.apache.http.conn.ssl.SSLSocketFactory;
Import org.apache.http.conn.ssl.X509HostnameVerifier;
Import org.apache.http.impl.client.DefaultHttpClient;
Import org.apache.http.message.BasicNameValuePair;
Import org.apache.http.util.EntityUtils;

/**
* Created by Huangjy on 2016-11-10.
*/
public class Httpstools {


/**
* Send HTTPS POST request
*
* @param
* @return Return response value
*/
public static string Sendhttpsrequestbypost (string url, map<string, string> Params) {
String responsecontent = null;
HttpClient HttpClient = new Defaulthttpclient ();
Create TrustManager
X509trustmanager XTM = new X509trustmanager () {
public void checkclienttrusted (x509certificate[] chain, String Authtype) throws certificateexception {
}

public void checkservertrusted (x509certificate[] chain, String Authtype) throws certificateexception {
}

Public x509certificate[] getacceptedissuers () {
Return null;
}
};
This is like a host Authentication.
X509hostnameverifier hostnameverifier = new X509hostnameverifier () {
Public boolean verify (String arg0, sslsession arg1) {
Return true;
}

public void Verify (String arg0, sslsocket Arg1) throws IOException {
}

public void Verify (String arg0, string[] arg1, string[] Arg2) throws sslexception {
}

public void Verify (String arg0, x509certificate Arg1) throws sslexception {
}
};
try {
TLS1.0 and SSL3.0 basically do not have much difference, can be roughly understood as TLS is the successor of ssl, but they use the same sslcontext
Sslcontext CTX = sslcontext.getinstance ("TLS");
Use TrustManager to initialize the context, TrustManager is only used by SSL sockets
Ctx.init (null, New trustmanager[]{xtm}, null);
Create Sslsocketfactory
Sslsocketfactory socketfactory = new Sslsocketfactory (ctx);
Socketfactory.sethostnameverifier (hostnameverifier);
Register sslsocketfactory with Schemeregistry on our httpclient
Httpclient.getconnectionmanager (). getschemeregistry (). Register (new Scheme ("https", socketfactory, 443));
HttpPost httppost = new HttpPost (url);
list<namevaluepair> formparams = new Arraylist<namevaluepair> (); Form parameters for building post requests
For (map.entry<string, string> Entry:params.entrySet ()) {
Formparams.add (new basicnamevaluepair (entry.getkey (), entry.getvalue ()));
}
Httppost.setentity (new urlencodedformentity (formparams, "UTF-8"));
HttpResponse response = Httpclient.execute (httppost);
httpentity entity = response.getentity (); Get response Entity
If (entity! = Null) {
Responsecontent = entityutils.tostring (entity, "UTF-8");
}
} catch (keymanagementexception E) {
E.printstacktrace ();
} catch (nosuchalgorithmexception E) {
E.printstacktrace ();
} catch (unsupportedencodingexception E) {
E.printstacktrace ();
} catch (clientprotocolexception E) {
E.printstacktrace ();
} catch (parseexception E) {
E.printstacktrace ();
} catch (ioexception E) {
E.printstacktrace ();
} finally {
Close the connection and release the resource
Httpclient.getconnectionmanager (). shutdown ();
}
Return responsecontent;
}

}

HTTPS Request Tool Class

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.