Android SSLHandshakeException-链验证失败,如何解决?
在我的应用程序中,我试图向我的服务器发出HTTPS POST请求。 然而,我一直得到SSLHandshakeException-链验证一直失败。我试图用邮递员发送请求,但得到了服务器的响应。当我尝试从应用程序发送请求时,是什么导致了此错误 下面是我尝试发送post请求的代码片段:Android SSLHandshakeException-链验证失败,如何解决?,android,https,sslhandshakeexception,Android,Https,Sslhandshakeexception,在我的应用程序中,我试图向我的服务器发出HTTPS POST请求。 然而,我一直得到SSLHandshakeException-链验证一直失败。我试图用邮递员发送请求,但得到了服务器的响应。当我尝试从应用程序发送请求时,是什么导致了此错误 下面是我尝试发送post请求的代码片段: public static JSONObject getDataLibConfiguration(Context context) throws HttpRequestException { int s
public static JSONObject getDataLibConfiguration(Context context) throws HttpRequestException {
int statusCode = 0;
JSONObject commonInformation;
HttpsURLConnection connection = null;
try {
commonInformation = ConfigurationProcessor.getCommonInformation(context);
if (commonInformation == null) {
return null;
}
URL url = new URL(BuildConfig.SERVER_CONFIG_URL);
if (BuildConfig.DEBUG) {
LogUtils.d(TAG, "url = " + url.getPath());
}
connection = getHttpsConnection(url);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
connection.setRequestProperty("Content-Encoding", "gzip");
byte[] gzipped = HttpUtils.gzip(commonInformation.toString());
cos = new CountingOutputStream(connection.getOutputStream()); //<-- This is where I get the exception
cos.write(gzipped);
cos.flush();
statusCode = connection.getResponseCode();
// More code her
}
private static HttpsURLConnection getHttpsConnection(URL url) throws IOException, GeneralSecurityException {
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
MatchDomainTrustManager myTrustManager = new MatchDomainTrustManager(url.getHost());
TrustManager[] tms = new TrustManager[]{myTrustManager};
sslContext.init(null, tms, null);
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
connection.setSSLSocketFactory(sslSocketFactory);
} catch (AssertionError ex) {
if (BuildConfig.DEBUG) {
LogFileUtils.e(TAG, "Exception in getHttpsConnection: " + ex.getMessage());
}
LogUtils.e(TAG, "Exception: " + ex.toString());
}
return connection;
}
公共静态JSONObject getDataLibConfiguration(上下文上下文)抛出HttpRequestException{
int statusCode=0;
JSONObject公共信息;
HttpsURLConnection连接=null;
试一试{
commonInformation=ConfigurationProcessor.getCommonInformation(上下文);
if(commonInformation==null){
返回null;
}
URL URL=新URL(BuildConfig.SERVER\u CONFIG\u URL);
if(BuildConfig.DEBUG){
LogUtils.d(标记,“url=“+url.getPath());
}
连接=getHttpsConnection(url);
connection.setDoOutput(真);
connection.setDoInput(true);
connection.setRequestMethod(“POST”);
setRequestProperty(“内容类型”,“应用程序/json;字符集=UTF-8”);
setRequestProperty(“内容编码”、“gzip”);
字节[]gzip=HttpUtils.gzip(commonInformation.toString());
cos=new CountingOutputStream(connection.getOutputStream());//问题在于证书已过期
public static void trustEveryone() {
try {
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier(){
public boolean verify(String hostname, SSLSession session) {
return true;
}});
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new X509TrustManager[]{new X509TrustManager(){
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}}}, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(
context.getSocketFactory());
} catch (Exception e) { // should never happen
e.printStackTrace();
}
}
或者检查设备的系统日期-当我尝试连接错误的日期时,出现了此异常。在我的情况下,手机上的日期是错误的
修复日期解决了一个问题我通过将api的基本url从“https”更改为“http”来修复它如果有人遇到与特定设备有关的此问题,那么原因应该是设备中设置的日期不正确。如果您使用的是模拟设备,只要“冷启动”它就可以解决问题
有时,如果让它们运行一段时间,这些东西上的日期可能会被卡住,从而导致证书过期问题。我的日期和时间是正确的,但我的系统设置中没有“检查使用网络提供的时间”
我通过进入设置>日期和时间>检查“使用网络提供的时间”和“使用网络提供的时区”来解决此问题
然后这个错误消失了。LogCat中的错误是什么?它只是说:“java.security.cert.CertificateException:链验证失败”但在我的情况下,它几乎在所有设备上都能工作,但很少有设备收到此错误消息。哪个证书?如何续订?@ahmadalibaloch我向其发送请求的服务器有一个证书已过期,因此我必须在GoDaddy上续订。在我的情况下,我的模拟器日期没有更新。几周后,我尝试使用此类代码发布更新由于安全原因,发布被谷歌阻止。请与我合作,谢谢这是一个非常糟糕的解决方法。你不需要使用不安全的协议来解决SSL握手问题。不能在一些自动重定向到https的网站上使用该技巧。我在安卓电视模拟器上也遇到了同样的问题。更新日期并设置正确的日期时区解决了这个问题。谢谢,伙计。保存我的一天。Glide创建了这个问题。这是我的解决方案。我刚刚将日期设置为“自动”,它就工作了。但是有没有办法让它与“手动”日期设置一起工作呢?@Krishnom,如果你在一加8上手动设置正确的时间,应该可以工作,两者都被检查了,但还是会发生错误。