Title, by default, HttpClient is not able to request HTTPS, you need to get
[Java]View Plaincopy
- Private static final int set_connection_timeout = 5 * 1000;
- private static final int set_socket_timeout = 1000;
- public static HttpClient getnewhttpclient () {
- try {
- KeyStore Truststore = keystore.getinstance (Keystore.getdefaulttype ());
- Truststore.load (null, null);
- Sslsocketfactory SF = new Mysslsocketfactory (Truststore);
- Sf.sethostnameverifier (Sslsocketfactory.allow_all_hostname_verifier);
- Httpparams params = new Basichttpparams ();
- Httpconnectionparams.setconnectiontimeout (params, 10000);
- Httpconnectionparams.setsotimeout (params, 10000);
- Httpprotocolparams.setversion (params, httpversion.http_1_1);
- Httpprotocolparams.setcontentcharset (params, HTTP. UTF_8);
- Schemeregistry Registry = new Schemeregistry ();
- Registry.register (new Scheme ("http", Plainsocketfactory.getsocketfactory (), 80));
- Registry.register (new Scheme ("https", SF, 443));
- Clientconnectionmanager CCM = New Threadsafeclientconnmanager (params, registry);
- Httpconnectionparams.setconnectiontimeout (params, set_connection_timeout);
- Httpconnectionparams.setsotimeout (params, set_socket_timeout);
- HttpClient client = new Defaulthttpclient (CCM, params);
- return client;
- } catch (Exception e) {
- return new Defaulthttpclient ();
- }
- }
Here is the Mysslsocketfactory class
[Java]View Plaincopy
- Private static class Mysslsocketfactory extends Sslsocketfactory {
- Sslcontext Sslcontext = sslcontext.getinstance ("TLS");
- Public mysslsocketfactory (KeyStore truststore) throws NoSuchAlgorithmException,
- Keymanagementexception, Keystoreexception, unrecoverablekeyexception {
- super (Truststore);
- TrustManager TM = 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;
- }
- };
- Sslcontext.init (null, new trustmanager[] {TM}, null);
- }
- @Override
- Public Socket Createsocket (socket socket, String host, int port, boolean autoClose)
- throws IOException, unknownhostexception {
- return Sslcontext.getsocketfactory (). Createsocket (socket, host, port, AutoClose);
- }
- @Override
- Public Socket Createsocket () throws IOException {
- return Sslcontext.getsocketfactory (). Createsocket ();
- }
- }