Android HttpClient 和 HTTPS [英] Android HttpClient and HTTPS
问题描述
我是在 Android 中实现 HTTPS 连接的新手.本质上,我正在尝试使用 org.apache.http.client.HttpClient 连接到服务器.我相信,在某些时候,我需要访问应用程序的密钥库才能使用私钥授权我的客户.但是,就目前而言,我只是尝试连接并看看会发生什么;我不断收到 HTTP/1.1 400 Bad Request 错误.
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.
尽管有很多例子(它们似乎都对我不起作用),但我似乎无法对此做出正面或反面.我的代码看起来像这样(BODY 常量是 XmlRPC):
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!
推荐答案
这应该会让你开始.我使用的基本相同,除了 ThreadSafeClientConnManager
.
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);
这篇关于Android HttpClient 和 HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!