- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试从 Andorid 4.1.1 版中的 Android 应用程序连接到 URL,并且我收到问题标题中指示的错误,但是当我尝试从 Andorid 4.0 版连接相同的 URL 时。 4 或 3.1,一切正常。
代码 fragment :
try {
.
.
.
URL url = new URL(urlStr);
Log.i(TAG,"[ URL ] " + urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
int size = conn.getContentLength();
int responsecode = conn.getResponseCode();
Log.d(TAG, "Responsecode: " + responsecode);
.
.
.
} catch (Exception e) {
e.printStackTrace();
}
private static void trustAllHosts() {
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[] {};
}
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
} };
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection
.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
System.out.println("IOException : HTTPSRequest::trustAllHosts");
e.printStackTrace();
}
}
但在这里我清楚一件事是“也许证书是自签名证书并且不包括在 KeyStore 中。
我不明白为什么这个异常只发生在 Android Verison 4.1.1 操作系统中谢谢。
全栈跟踪
01-31 10:26:08.348: W/System.err(3158): java.io.IOException: Hostname <URL> was not verified
01-31 10:26:08.348: W/System.err(3158): at libcore.net.http.HttpConnection.verifySecureSocketHostname(HttpConnection.java:223)
01-31 10:26:08.348: W/System.err(3158): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:446)
01-31 10:26:08.348: W/System.err(3158): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
01-31 10:26:08.348: W/System.err(3158): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
01-31 10:26:08.348: W/System.err(3158): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
01-31 10:26:08.348: W/System.err(3158): at libcore.net.http.HttpURLConnectionImpl.getHeaderField(HttpURLConnectionImpl.java:130)
01-31 10:26:08.348: W/System.err(3158): at java.net.URLConnection.getHeaderFieldInt(URLConnection.java:544)
01-31 10:26:08.348: W/System.err(3158): at java.net.URLConnection.getContentLength(URLConnection.java:316)
01-31 10:26:08.348: W/System.err(3158): at libcore.net.http.HttpsURLConnectionImpl.getContentLength(HttpsURLConnectionImpl.java:191)
01-31 10:26:08.348: W/System.err(3158): at com.ih.util.HelpVideoServices$downloadTask.run(HelpVideoServices.java:172)
最佳答案
如果您使用没有任何意义的证书运行并且您想绕过它们,您还需要添加一个空主机名验证器以使此代码正常工作
HttpsURLConnection.setDefaultHostnameVerifier(new NullHostNameVerifier());
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new X509TrustManager[]{new NullX509TrustManager()}, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
以及主机的代码:
import javax.net.ssl.HostnameVerifier ;
import javax.net.ssl.SSLSession;
public class NullHostNameVerifier implements HostnameVerifier {
@Override
public boolean verify(String hostname, SSLSession session) {
Log.i("RestUtilImpl", "Approving certificate for " + hostname);
return true;
}
}
这需要运行一次,但如果您要更改连接对象,您可能需要再次运行它。
关于android - java.io.IOException : Hostname was not verified,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14619781/
我一直在开发数字签名 Node.js 应用程序,然后我遇到了我无法弄清楚的奇怪行为。 首先我生成了公钥/私钥对 described here 然后我实现了以下脚本 const crypto = r
缺少[路由:验证。通知] [URI:{language} / email / verify]所需的参数 使用本地化后,我将laravel电子邮件验证添加到了我的项目中。 但是现在我遇到了问题:Rout
我用对象列表调用方法 performAction 并验证相同的方法。调用此方法后,我修改了一些“对象”。 Mockito 验证失败,说参数不匹配(显示修改的对象),但我可以在 Debug模式下看到对象
我在处理opensl时遇到麻烦,提示无法在具有CA链的地方验证本地颁发的证书。除了本地CA链证书(CER,PEM,CRT)之外,我还有本地颁发的证书(PEM和CRT)。根和颁发者是同一台服务器。比较两
我正在使用 protobuf 自动生成我的 API 服务,在 gRPC 中技术,在我的安卓客户端。自从我一直在毫无问题地工作以来,已经过去了几个月。现在突然之间,AndroidRuntime Veri
我们目前正在使用“mvn clean verify”在 Hudson 上运行我们的持续构建。这就是我们一直在做的,所以我们从不质疑它。 问题是:仅使用“mvn verify”运行连续构建是否安全?因此
我已经在我的虚拟机上安装了 redis。然后我运行命令 [root@vm001jeguan ~]# rladmin help verify 它显示以下错误: [root@dltasvm001jegua
我正在使用以下应用链接:https://developer.android.com/training/app-links/verify-site-associations 使用cmd时:adb she
我写了一个应用程序,我想在网上销售它,所以我把它上传到我的网站,并试图下载并运行它作为测试,但出现了一个窗口,它说:“无法验证发布者。你确定吗?要运行此软件吗?”,并且还说:“发布者:未知发布者”。它
我们正在迁移与Kotlin接触的类,因此我们需要在Java测试类中模拟Kotlin类。我们使用io.mockk:mockk:1.9.3。 为了至少验证x次,我们有一个包装器方法 public stat
在完全更新的 XCode 4.6(以及之前的 4.5)中,构建过程的“验证应用程序”步骤在我的机器上通常需要两分钟以上的时间。在类似计算机上构建相同应用程序的同事只需几秒钟即可完成此步骤。在过去的几个
它在 documentation 中说 构建生命周期中的验证阶段 run any checks on results of integration tests to ensure quality cr
我有一个模拟对象,正在验证一些调用的东西。但想知道 MockedObject.Verify(thingToBeVerified) 的默认参数是什么? 是 Times.Zero、Times.Once 还
谁能向我解释一下每种用户类型(荣誉、已验证、审核和员工之间的区别)他们的角色? (对每个用户及其可以执行的操作的简要说明)。 我查了官方文档,但没有找到! 谢谢。 最佳答案 Honor 用户会自动注册
我正在编写一个 Chrome 扩展,其中内容脚本向后台脚本发送消息。 chrome.runtime.sendMessage({greeting: "hello"}, function(response
我在编写测试时已经习惯了遵循代码模式 public void TestMethod_Condition_Output() { //Arrange---------------- Moc
我有以下代码: class Foo { public Foo() { Size = true; } private bool _size; pr
我有以下代码,取自 ShowSignature来自 PDFBox 2 的示例。我正在运行五个测试,其中调用了它,其中 4 个测试顺利通过(无签名、单签名、双签名、过期签名),但第五个测试是椭圆曲线,但
我正在尝试mockito.verify()功能,该方法仅调用一次,但verify显示调用3次。单元测试只有一个测试,以确保该测试不会受到其他影响。Mockito.verify(utilClass, M
摆弄 Mockito 来实现我的服务的单元测试,但出于某种原因,我无法通过我厚实的头骨来完成它。我的测试通过了,但我不相信我做对了。 这是我测试 count() 方法的示例。该方法只是将调用转发到其存
我是一名优秀的程序员,十分优秀!