Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid HTTPS Post - Not working
    primarykey
    data
    text
    <p>I've been trying for ages to get this to work - but no matter what I do, my HTTP*<em>S</em>* POST always yields <code>HttpUtils: javax.net.ssl.SSLException: Not trusted server certificate</code></p> <p>Basically I followed this <a href="http://blog.crazybob.org/2010/02/android-trusting-ssl-certificates.html" rel="nofollow">tutorial</a></p> <ul> <li>I successfully grabbed the public certificate (mycert.pem) from the server. </li> <li>I successfully created a keystore from the certificate using Bouncy Castle </li> <li><p>I failed at implementing a custom Apache HttpClient. Here is my code:</p> <pre><code>import android.content.Context; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.scheme.PlainSocketFactory; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.SingleClientConnManager; import org.apache.http.params.HttpParams; import java.io.InputStream; import java.security.KeyStore; public class MyHttpClient extends DefaultHttpClient { final Context context; public MyHttpClient(Context context) { this.context = context; } public MyHttpClient(Context context2, HttpParams myParams) { super(myParams); this.context= context2; } @Override protected ClientConnectionManager createClientConnectionManager() { SchemeRegistry registry = new SchemeRegistry(); registry.register( new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", newSslSocketFactory(), 443)); return new SingleClientConnManager(getParams(), registry); } private SSLSocketFactory newSslSocketFactory() { try { KeyStore trusted = KeyStore.getInstance("BKS"); InputStream in = context.getResources().openRawResource(R.raw.mystore); try { trusted.load(in, "password".toCharArray()); } finally { in.close(); } return new SSLSocketFactory(trusted); } catch (Exception e) { throw new AssertionError(e); } } } </code></pre> <p>And in my HTTP Request class that constructs the POST:</p> <pre><code>public class HttpRequest { MyHttpClient httpClient; HttpContext localContext; private String ret; HttpResponse response = null; HttpPost httpPost = null; HttpGet httpGet = null; public HttpRequest(Context context){ HttpParams myParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(myParams, 10000); HttpConnectionParams.setSoTimeout(myParams, 10000); httpClient = new MyHttpClient(context, myParams); localContext = new BasicHttpContext(); } public String sendPost(String url, String data, String contentType) { ret = null; httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109); httpPost = new HttpPost(url); response = null; StringEntity tmp = null; httpPost.setHeader("User-Agent", "SET YOUR USER AGENT STRING HERE"); httpPost.setHeader("Accept", "text/html,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); if (contentType != null) { httpPost.setHeader("Content-Type", contentType); } else { httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); } try { tmp = new StringEntity(data,"UTF-8"); } catch (UnsupportedEncodingException e) { Log.e("Log", "HttpUtils : UnsupportedEncodingException : "+e); } httpPost.setEntity(tmp); try { response = httpClient.execute(httpPost,localContext); if (response != null) { ret = EntityUtils.toString(response.getEntity()); } } catch (Exception e) { Log.e("Log", "HttpUtils: " + e); } return ret; } } </code></pre></li> </ul> <p>My POST works fine for non-https websites. Any help would be greatly appreciated it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload