Answer the question
In order to leave comments, you need to log in
HTTPS request in Android?
I'm trying to send a POST request via SSL to Android. The code is like this:
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new SSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
httpParams = new BasicHttpParams();
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(httpParams, HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(httpParams, true);
SchemeRegistry sr = new SchemeRegistry();
sr.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
sr.register(new Scheme("https", sf, 443));
ccManager = new ThreadSafeClientConnManager(httpParams, sr);
HttpClient client = = new DefaultHttpClient(ccManager, httpParams);
HttpPost post = new HttpPost(url);
post.setEntity(new UrlEncodedFormEntity(params));
HttpResponse responce = client.execute(post);
in = new BufferedReader(new InputStreamReader(responce.getEntity().getContent()));
String line = "";
while ((line = in.readLine()) != null) sb.append(line);
in.close();
Answer the question
In order to leave comments, you need to log in
Answer to my question:
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
httpParams = new BasicHttpParams();
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(httpParams, HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(httpParams, true);
SchemeRegistry sr = new SchemeRegistry();
sr.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
sr.register(new Scheme("https", sf, 443));
ccManager = new ThreadSafeClientConnManager(httpParams, sr);
HttpClient client = = new DefaultHttpClient(ccManager, httpParams);
HttpPost post = new HttpPost(url);
post.setEntity(new UrlEncodedFormEntity(params));
HttpResponse responce = client.execute(post);
in = new BufferedReader(new InputStreamReader(responce.getEntity().getContent()));
String line = "";
while ((line = in.readLine()) != null) sb.append(line);
in.close();
public class MySSLSocketFactory extends SSLSocketFactory {
private 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();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question