Android HttpClient 和 HTTPS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2603691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android HttpClient and HTTPS
提问by harrisonlee
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.
我是在 Android 中实现 HTTPS 连接的新手。本质上,我正在尝试使用 org.apache.http.client.HttpClient 连接到服务器。我相信,在某些时候,我需要访问应用程序的密钥库才能使用私钥授权我的客户。但是,就目前而言,我只是尝试连接并看看会发生什么;我不断收到 HTTP/1.1 400 Bad Request 错误。
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):
尽管有很多例子(它们似乎都不适合我),我似乎无法对此做出正面或反面。我的代码看起来像这样(BODY 常量是 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!
所以,很简单。如果有人有任何建议,请告诉我。谢谢!
回答by synic
This should get you started. I'm using basically the same, except with ThreadSafeClientConnManager
.
这应该让你开始。我使用的基本相同,除了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);