Android HttpClient and HTTPS

别说谁变了你拦得住时间么 提交于 2019-11-26 12:07:18

问题


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!


回答1:


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);


来源:https://stackoverflow.com/questions/2603691/android-httpclient-and-https

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!