Search This Blog

Wednesday, April 10, 2013

android - How to post data to not verified SSL url

the next code snippets will  allow you to make HTTPS call to non verified SSL certificate.

 HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;  
 HttpClient client = new DefaultHttpClient();  
 SchemeRegistry registry = new SchemeRegistry();  
 SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();  
 socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);  
 registry.register(new Scheme("https", socketFactory, 443));  
 SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);  
 DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());  
 HttpPost post = new HttpPost("https://www.yaniv.co.il/tst/IsValidUser.php");  
 List<NameValuePair> pairs = new ArrayList<NameValuePair>();  
 pairs.add(new BasicNameValuePair("user_name", user));  
 pairs.add(new BasicNameValuePair("password", pwd));  
 pairs.add(new BasicNameValuePair("type", type));                 
 post.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8));  
 HttpParams params = new BasicHttpParams();  
 int timeoutConnection = 5000;  
 HttpConnectionParams.setConnectionTimeout(params, timeoutConnection);  
 post.setParams(params);  
 HttpResponse response = httpClient.execute(post);  

be aware ,its not safe to make calls to non verified ssl, unless you know what are you doing.

enjoy
yaniv tzanany

No comments: