HTTPS and self-signed certificate issue?

I finally solved it, using a custom subclass of SSLSocketFactory: public class CustomSSLSocketFactory extends SSLSocketFactory { private SSLContext sslContext = SSLContext. GetInstance("TLS"); public CustomSSLSocketFactory(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(); } } And I use it as follows: public HttpClient getHttpClient() { DefaultHttpClient client = null; try { KeyStore trustStore = KeyStore.

GetInstance(KeyStore.getDefaultType()); trustStore. Load(null, null); SSLSocketFactory sf = new CustomSSLSocketFactory(trustStore); sf. SetHostnameVerifier(SSLSocketFactory.

ALLOW_ALL_HOSTNAME_VERIFIER); // Setting up parameters HttpParams params = new BasicHttpParams(); HttpProtocolParams. SetVersion(params, HttpVersion. HTTP_1_1); HttpProtocolParams.

SetContentCharset(params, "utf-8"); params. SetBooleanParameter("http.protocol. Expect-continue", false); // Setting timeout HttpConnectionParams.

SetConnectionTimeout(params, TIMEOUT); HttpConnectionParams. SetSoTimeout(params, TIMEOUT); // Registering schemes for both HTTP and HTTPS SchemeRegistry registry = new SchemeRegistry(); registry. Register(new Scheme("http", PlainSocketFactory.

GetSocketFactory(), 80)); registry. Register(new Scheme("https", sf, 443)); // Creating thread safe client connection manager ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); // Creating HTTP client client = new DefaultHttpClient(ccm, params); // Registering user name and password for authentication client. GetCredentialsProvider().

SetCredentials( new AuthScope(null, -1), new UsernamePasswordCredentials(mUsername, mPassword)); } catch (Exception e) { client = new DefaultHttpClient(); } return client; } Don't know why the other solutions I found did not work for me.

I finally solved it, using a custom subclass of SSLSocketFactory: public class CustomSSLSocketFactory extends SSLSocketFactory { private SSLContext sslContext = SSLContext. GetInstance("TLS"); public CustomSSLSocketFactory(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(); } } And I use it as follows: public HttpClient getHttpClient() { DefaultHttpClient client = null; try { KeyStore trustStore = KeyStore.

GetInstance(KeyStore.getDefaultType()); trustStore. Load(null, null); SSLSocketFactory sf = new CustomSSLSocketFactory(trustStore); sf. SetHostnameVerifier(SSLSocketFactory.

ALLOW_ALL_HOSTNAME_VERIFIER); // Setting up parameters HttpParams params = new BasicHttpParams(); HttpProtocolParams. SetVersion(params, HttpVersion. HTTP_1_1); HttpProtocolParams.

SetContentCharset(params, "utf-8"); params. SetBooleanParameter("http.protocol. Expect-continue", false); // Setting timeout HttpConnectionParams.

SetConnectionTimeout(params, TIMEOUT); HttpConnectionParams. SetSoTimeout(params, TIMEOUT); // Registering schemes for both HTTP and HTTPS SchemeRegistry registry = new SchemeRegistry(); registry. Register(new Scheme("http", PlainSocketFactory.

GetSocketFactory(), 80)); registry. Register(new Scheme("https", sf, 443)); // Creating thread safe client connection manager ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); // Creating HTTP client client = new DefaultHttpClient(ccm, params); // Registering user name and password for authentication client. GetCredentialsProvider().

SetCredentials( new AuthScope(null, -1), new UsernamePasswordCredentials(mUsername, mPassword)); } catch (Exception e) { client = new DefaultHttpClient(); } return client; } Don't know why the other solutions I found did not work for me...

Try this before performing the request SSLSocketFactory ssl = (SSLSocketFactory)http. GetConnectionManager(). GetSchemeRegistry().

GetScheme( "https" ). GetSocketFactory(); ssl. SetHostnameVerifier( SSLSocketFactory.

ALLOW_ALL_HOSTNAME_VERIFIER ).

Sorry, you're alreay doing that in a similar way, but it works for me... – 2red13 May 10 at 8:24.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions