32

I'm new to implementing HTTPS connections in Android. Essentially, I'm trying to connect to a server using the org.apache.http.client.HttpClient. I believe, at some point, I'll need to access the application's keystore in order to authorize my client with a private key. But, for the moment, I'm just trying to connect and see what happens; I keep getting an HTTP/1.1 400 Bad Request error.

I can't seem to make heads or tails of this despite many examples (none of them seem to work for me). My code looks like this (the BODY constant is XmlRPC):

 private void connect() throws IOException, URISyntaxException{

    HttpPost post     = new HttpPost(new URI(PROD_URL));
    HttpClient client = new DefaultHttpClient();

    post.setEntity(new StringEntity(BODY));
    HttpResponse result = client.execute(post);

    Log.d("MainActivity", result.getStatusLine().toString());

}

So, pretty simple. Let me know if anyone out there has any advice. Thanks!

harrisonlee
  • 5,068
  • 4
  • 21
  • 20

1 Answers1

18

This should get you started. I'm using basically the same, except with ThreadSafeClientConnManager.

SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("https", 
            SSLSocketFactory.getSocketFactory(), 443));

HttpParams params = new BasicHttpParams();

SingleClientConnManager mgr = new SingleClientConnManager(params, schemeRegistry);

HttpClient client = new DefaultHttpClient(mgr, params);
synic
  • 26,359
  • 20
  • 111
  • 149
  • 2
    Thanks for your quick response. I actually tried a similar approach earlier with no luck; same bad request error. – harrisonlee Apr 09 '10 at 00:34
  • Then the problem isn't with HTTPS, it's something else. The code I pasted works for me. – synic Apr 09 '10 at 00:42
  • Just for clarification: it looks like you're using org.apache.http.conn.ssl.SSLSocketFactory , which is not the same as javax.net.ssl.SSLSocketFactory . Is that correct? – Dalbergia Aug 22 '11 at 20:09
  • @synic hi synic if you know anything about how can we validate the keystore with truststore process please let me know.. that could be helpful for me..thanks in advance... – Quick learner Mar 02 '12 at 07:15
  • @Dalbergia yes it is org.apache.http.conn.ssl.SSLSocketFactory (I had to frown quite some time before realizing). – viphe Feb 13 '13 at 19:13