gpt4 book ai didi

android - 如何创建 https 连接?

转载 作者:行者123 更新时间:2023-11-30 04:03:19 25 4
gpt4 key购买 nike

我的代码适用于 http,但不适用于 https。我正在尝试使用 HttpClient 在 Android 手机上建立 Https 连接。问题是我不断收到 net.ssl.SSLPeerUnverifiedException: No peer certificate。和 Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found。

这里是自定义HttpClient的相关代码。

public static HttpClient getNewHttpClient() {

HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(params, true);

SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);

DefaultHttpClient http = new DefaultHttpClient(conMgr, params);
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("name", "pass");
AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT);
http.getCredentialsProvider().setCredentials(authScope, credentials);

return http;
}

这是从服务器获取信息的代码

public static void Get() {

HttpClient http = getNewHttpClient();

HttpResponse response;
try {
HttpGet httpost = new HttpGet(url);
response = http.execute(httpost);
BufferedReader in = null;
Log.i(TAG, "resp-" + response);
in = new BufferedReader(new InputStreamReader(response.getEntity()
.getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String page = sb.toString();
Log.i(TAG, "res=" + sb.toString());
tv.setText(page);
} catch (ClientProtocolException e) {
Log.i(TAG, "ClientProtocolException=" + e);
e.printStackTrace();
} catch (IOException e) {
Log.i(TAG, "ClientProtocolException=" + e);
e.printStackTrace();
}
}

有什么想法吗?

最佳答案

终于我得到了我的问题的解决方案,对于这个解决方案我在三天内搜索了很多。使用用户名和密码对服务器进行身份验证时出现问题。我像这样更改了代码。我正在将凭据传递给服务器,这只是我的代码发生了变化,而不是问题中可用的代码..

public static HttpClient _getNewHttpClient() {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);

SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_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);

DefaultHttpClient http = new DefaultHttpClient(ccm, params);
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("jk", "jk");
AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT);
http.getCredentialsProvider().setCredentials(authScope, credentials);

return http;
} catch (Exception e) {
return new DefaultHttpClient();
}
}

关于android - 如何创建 https 连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12136907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com