gpt4 book ai didi

Android 通配符 SSL 验证

转载 作者:太空狗 更新时间:2023-10-29 13:38:51 25 4
gpt4 key购买 nike

<分区>

我需要一些帮助来验证我的 Android 应用程序中的 WildCard SSL。现在我正在使用无需检查任何内容即可验证的代码:

public void UseHttpsConnection(String url, String charset, String query) {

try {
final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
@Override
public void checkClientTrusted( final X509Certificate[] chain, final String authType ) {
}
@Override
public void checkServerTrusted( final X509Certificate[] chain, final String authType ) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
} };

// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance( "TLS" );
sslContext.init( null, trustAllCerts, new java.security.SecureRandom() );
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();



System.setProperty("http.keepAlive", "false");
HttpsURLConnection connection = (HttpsURLConnection) new URL(url)
.openConnection();
connection.setSSLSocketFactory( sslSocketFactory );
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Charset", charset);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=" + charset);
OutputStream output = null;
try {
output = connection.getOutputStream();
output.write(query.getBytes(charset));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (output != null)
try {
output.close();
} catch (IOException logOrIgnore) {
logOrIgnore.printStackTrace();
}
}

int status = ((HttpsURLConnection) connection).getResponseCode();
Log.i("", "Status : " + status);

for (Entry<String, List<String>> header : connection
.getHeaderFields().entrySet()) {
Log.i("Headers",
"Headers : " + header.getKey() + "="
+ header.getValue());
}

InputStream response = new BufferedInputStream(
connection.getInputStream());

int bytesRead = -1;
byte[] buffer = new byte[30 * 1024];
while ((bytesRead = response.read(buffer)) > 0) {
byte[] buffer2 = new byte[bytesRead];
System.arraycopy(buffer, 0, buffer2, 0, bytesRead);
handleDataFromSync(buffer2);
}
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}

当我通过互联网阅读时,我发现 Android 中存在验证 WildCard(示例:*.mydomain.com)证书的错误,但我没有找到任何示例或建议如何做到这一点。我想要实现的是:通过名称验证证书并检查它是否已过期。欢迎任何好的建议/示例,因为这是我第一次尝试使用 SSL,而且我并不十分熟悉它。

提前致谢!

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