我正在尝试使用HttpClientlib 进行HTTPS连接,但问题在于,由于证书未由Verisign,GlobalSIgn等公认的证书颁发机构(CA)签署,并列在Android受信任证书集上,我一直在javax.net.ssl.SSLException: Not trusted server certificate.
我已经看到了你只接受所有证书的解决方案,但如果我想询问用户该怎么办?
我想得到一个类似于浏览器的对话框,让用户决定是否继续.我最好使用与浏览器相同的证书库.有任何想法吗?
我想将https请求发送到我自己的服务器站点https://10.2.20.20/fido/EzPay/login.php并从中获取响应,并将其保存为例如字符串.我在互联网上找到了一些示例代码,并尝试测试它们以解决我的问题,但它们没有帮助.下面我介绍一些我测试过的代码部分.
我试试这段代码,但我总是得到同样的例外"没有同行证书"为什么?
try
{
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
DefaultHttpClient client = new DefaultHttpClient();
SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("https", socketFactory, 443));
SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());
// Set verifier
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
// Example send http request
final String url = "https://10.2.20.20/fido/EzPay/login.php";
HttpPost httpPost = new HttpPost(url);
HttpResponse response = httpClient.execute(httpPost);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = …Run Code Online (Sandbox Code Playgroud) 每当此代码运行时,我都会收到"No Peer Certificate"错误.
SSL证书有效,从Namecheap(PositiveSSL)购买.它之前有CA crt,并且在Android浏览器中打开正常.
HTTP服务器:nginx
码:
public void postData() {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("string", "myfirststring"));
try {
HttpPost post = new HttpPost(new URI("https://example.com/submit"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
KeyStore trusted = KeyStore.getInstance("BKS");
trusted.load(null, "".toCharArray());
SSLSocketFactory sslf = new SSLSocketFactory(trusted);
sslf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme ("https", sslf, 443));
SingleClientConnManager cm = new SingleClientConnManager(post.getParams(),
schemeRegistry);
HttpClient client = new DefaultHttpClient(cm, post.getParams());
// Execute HTTP Post Request
@SuppressWarnings("unused")
HttpResponse result = client.execute(post);
} catch (ClientProtocolException …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Apache 的 HttpClient 4.X 从 Android 应用程序查询 HTTPS 网站。我得到一个javax.net.ssl.SSLPeerUnverifiedException: No peer certificate例外。我按照不同的说明导入证书:
这些都不适合我。
我认为我的问题是该站点的证书链如下:
所述电信根CA 2被包括在Android默认密钥库(/system/etc/security/cacerts.bks)。我在自己用于测试的手机上检查了这一点。
现在的问题是,我必须在自定义密钥库中包含所有说明告诉我提供哪些证书?只是中间件?只有网站吗?两个都?我也需要根证书吗?不Site's certificate应该由链验证 - 我不需要提供任何额外的证书,因为链的根是可信的。
如果您能向我解释发生了什么以及我如何使其发挥作用,我会很高兴。请不要发布忽略证书的解决方法,因为这是用于生产并且必须是安全的。