关于androidhttpclient的信息很少,特别是我找不到任何好的例子。从我读到的-我可以使用这个客户端,它是为ssl预先配置的。我的目标是2.2+,所以这对我很有用。
关于我如何使用它有什么好的例子吗?专为休息服务岗
有没有关于如何允许自签名证书的示例?我不介意只允许任何证书而不是将特定的证书导入本地存储。
谢谢!
我自己的答案(见下面的代码)。
我有具有自签名证书的IIS服务器。我必须执行额外的步骤,生成与外部名称(而不是服务器名称)匹配的证书。
我用androidhttpclient。据推测,这个客户机拥有Android的所有“正确”设置,并从版本8开始支持
我在应用程序对象中创建androidhttpclient并在中共享。
我在插入自定义证书的地方分离了代码,这样以后就可以很容易地删除它。我注意到在应用程序启动时,从资源加载证书确实需要时间。
我的应用程序单例版。查看上面的注释和我用来生成所有内容的命令行的详细信息。始终使用相同的密码以确保其正常工作。pks文件密码必须匹配。
import android.net.http.AndroidHttpClient;
import android.app.Application;
import android.util.Log;
import idatt.mobile.android.providers.DBLog;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import java.io.InputStream;
import java.security.KeyStore;
/*
To generate PKS:
1. Created cert in IIS7 and then exported as pfx. Follow instruction on SelfSSL: http://www.robbagby.com/iis/self-signed-certificates-on-iis-7-the-easy-way-and-the-most-effective-way/
1a. Download tool: http://cid-3c8d41bb553e84f5.skydrive.live.com/browse.aspx/SelfSSL
1b. Run: SelfSSL /N:CN=mydomainname /V:1000 /S:1 /P:8081
I use port 8081 on my server
1c. Export from IIS manager to cert.pfx
2. Run command line in SSL to convert file into X.509:
openssl pkcs12 -in C:\cert.pfx -out C:\cert.cer -nodes
3. Edit file and delete all except -----BEGIN.... END CERTIFICATE----- IMPORTANT! It was working when I got proper (5) amount of dashes and put tags and data on separate lines
4. use keytool. C:\Java\JDK\bcprov.jar was downloaded separately
C:\Users\Ivan>keytool -import -v -trustcacerts -alias key_alias -file C:\cert.cer -keystore C:\mystore.bks -storetype BKS -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath C:\Java\JDK\bcprov.jar -storepass 123456
*/
public class MyApplication extends Application
{
private static final String LOG_TAG = "MyApplication";
private AndroidHttpClient androidHttpClient;
@Override
public void onCreate()
{
super.onCreate();
androidHttpClient = createAndroidHttpClient();
}
@Override
public void onLowMemory()
{
super.onLowMemory();
shutdownAndroidHttpClient();
}
@Override
public void onTerminate()
{
super.onTerminate();
shutdownAndroidHttpClient();
}
private AndroidHttpClient createAndroidHttpClient()
{
Log.d(LOG_TAG,"createAndroidHttpClient");
AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
//This is optional call to inject custom BKS that was created from self-signed certificate
client = addCustomCertificate(client);
return client;
}
public AndroidHttpClient getAndroidHttpClient()
{
return androidHttpClient;
}
private void shutdownAndroidHttpClient()
{
if(androidHttpClient!=null && androidHttpClient.getConnectionManager()!=null)
{
androidHttpClient.getConnectionManager().shutdown();
}
}
private AndroidHttpClient addCustomCertificate(AndroidHttpClient client)
{
SSLSocketFactory sf = SSLSocketFactory.getSocketFactory();
try
{
InputStream in = getResources().openRawResource(R.raw.home_server);
KeyStore trustStore = KeyStore.getInstance("BKS");
trustStore.load(in, "123456".toCharArray());
in.close();
sf = new SSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
}
catch (Exception t)
{
DBLog.InsertError(this, t);
}
//Lets register our custom factory here
client.getConnectionManager().getSchemeRegistry().register(new Scheme("https", sf, 443));
return client;
}
}
下面是我如何使用这个客户机(我在asynctask中调用它)
private String processPOST(String url, String requestData)
{
String responseData = null;
application = (MyApplication)getApplication();
AndroidHttpClient client = application.getAndroidHttpClient();
HttpPost request = new HttpPost(url);
try
{
StringEntity entity = new StringEntity(requestData);
entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
request.setEntity(entity);
ResponseHandler<String> handler = new BasicResponseHandler();
responseData = client.execute(request, handler);
}
catch (Throwable e)
{
DBLog.InsertError(ctxt, e);
}
return responseData;
}
这种组合似乎是100%在2.2和2.3设备上工作。当我在DefaultHttpClient中使用代码片段时,我遇到了2.3.1请求超时(Nexus S)的问题。
最佳答案
您可以使用apache httpclient。
public HttpClient getNewHttpClient() {
try {
KeyStore trustStore = KeyStore.getInstance("BKS");
InputStream in = getResources().openRawResource(R.raw.mykeystore);
try {
trustStore.load(in, "mypassword".toCharArray());
} finally {
in.close();
}
SSLSocketFactory sf = new SSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
return new DefaultHttpClient(ccm, params);
} catch (Exception e) {
return new DefaultHttpClient();
}
}
在web服务器中,iis可以创建自签名证书并导出为pfx,然后使用openssl工具将其转换为pem,编辑为conatin-only证书,然后使用jdk的keytool和bouncy castle jar创建包含证书的密钥库。创建的密钥库可以导入到项目中,如上面的代码所示。
关于android - AndroidHttpClient需要更多信息。自签名SSL。可能?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5654825/