AndroidHttpClient需要更多信息.自签名SSL.可能?

AndroidHttpClient需要更多信息.自签名SSL.可能?,第1张

概述关于AndroidHttpClient的信息很少,特别是我找不到任何好的示例.根据我的阅读-我可以使用此客户端,并且已针对SSL对其进行了预配置.我的目标是2.2,因此对我来说效果很好.>是否有使用我的好样本?专门用于REST服务POST>是否有关于如何允许自签名证书的示例?我不介意只允许使用任何证书

关于AndroidhttpClIEnt的信息很少,特别是我找不到任何好的示例.根据我的阅读-我可以使用此客户端,并且已针对SSL对其进行了预配置.我的目标是2.2,因此对我来说效果很好.

>是否有使用我的好样本?专门用于REST服务POST
>是否有关于如何允许自签名证书的示例?我不介意只允许使用任何证书,而不是将特定证书导入本地商店.

谢谢!

我自己的答案(请参见下面的代码).

>我有带有自签名证书的IIS服务器.我不得不采取额外的步骤,并生成与外部名称而不是服务器名称匹配的证书.
>我使用androidhttpclient.据说,此客户端具有适用于androID的所有“正确”设置,并且从版本8开始受支持
>我在Application对象中创建androidhttpclient并共享.
>我在注入自定义证书的地方分离了代码,因此以后可以轻松删除它.我注意到从资源加载证书确实需要花时间在App启动上.

我的应用程序单例版本.请参阅顶部的注释,以及我用来生成所有内容的命令行的详细信息.在整个过程中使用相同的密码以确保其有效. 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/SelfSSL1b. Run: SelfSSL /N:CN=mydomainname /V:1000 /S:1 /P:8081 I use port 8081 on my server1c. Export from IIS manager to cert.pfx2. Run command line in SSL to convert file into X.509:openssl pkcs12 -in C:\cert.pfx -out C:\cert.cer -nodes3. 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 lines4. 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,将其编辑为仅包含证书,然后使用JDK和Bouncy Castle jar的密钥工具创建一个包含证书的密钥库.如上面的代码所示,可以将创建的密钥库导入到您的项目中.

总结

以上是内存溢出为你收集整理的AndroidHttpClient需要更多信息.自签名SSL.可能?全部内容,希望文章能够帮你解决AndroidHttpClient需要更多信息.自签名SSL.可能?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1071329.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存