gpt4 book ai didi

android - 通过 https 信任自签名证书

转载 作者:太空宇宙 更新时间:2023-11-03 15:04:03 25 4
gpt4 key购买 nike

我知道网上有很多教程,但是它们超出了我的知识范围,因为我是第一次使用https。我在这里使用了emmby的答案Trusting all certificates using HttpClient over HTTPS .但是我不知道在我连接到服务器的类里面进一步的实现是如何进行的。这是我的 HttpsConection 类中的代码 fragment

  Log.d("url", url.toString());
HttpsURLConnection httpsConnection;

Log.d("HTTP get", "get() called");
try
{
Log.v("HttpConnection", url.toString());
httpsConnection = (HttpsURLConnection) url.openConnection();

if (request != null)
{
OutputStreamWriter wr = new OutputStreamWriter(
httpsConnection.getOutputStream());
// Log.e(TAG, "created outputstream");

wr.write(request);
// Log.e(TAG, "request sent");
wr.flush();
wr.close();
} else
{
Log.e("HttpConnection", "Nothing to send to server");
}

// Execute
try
{

InputStream in = new BufferedInputStream(httpsConnection
.getInputStream());
responseString = convertStreamToString(in);
in.close();

我的 res/raw 文件夹中有一个 *.bks 文件,但我卡在那里了。

最佳答案

这是我的自签名 https 连接代码,对我来说工作正常:

        KeyStore trustStore;
try {
trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new EasySSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
BasicHttpParams params = new BasicHttpParams();
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 httpClient = new DefaultHttpClient(ccm, params);
HttpsURLConnection.setDefaultHostnameVerifier(sf.getHostnameVerifier());
HttpGet getRequest = new HttpGet(url);
BasicHttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 10000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 15000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
httpClient.setParams(httpParameters);
HttpResponse getResponse = httpClient.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK)
{
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
String content = EntityUtils.toString(getResponseEntity);
InputStream is = new ByteArrayInputStream(content.getBytes("UTF-8"));
return is;
} catch (KeyStoreException e1) {
e1.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
}
return null;

关于android - 通过 https 信任自签名证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14959469/

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