- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
编辑:评论中的 BNK 已链接到找到的解决方案 here .
我正在通过 REST 将 POST 请求发送到后端服务器(通过 LAN),所有这些都是通过 HTTPS 完成的。此服务器有一个自签名证书作为 .pem 文件,一切正常。
我现在正在尝试连接到不同的 Web 服务器(通过 WAN,通过 DNS),一个自签名证书也是一个 .crt 文件(标准,BER/DER 格式)。但是现在,尽管代码相同,但我收到以下异常:
java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
我不确定为什么一台服务器可以连接但另一台服务器不行。我不想信任所有证书,因为这将通过公共(public)互联网进行。
我的网络代码:
public HttpsURLConnection setUpHttpsConnection(String urlString)
{
try
{
// Load CAs from an InputStream
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream caInput = new BufferedInputStream(context.getAssets().open("server.crt"));
Certificate ca = cf.generateCertificate(caInput);
System.out.println("ca=" + ((java.security.cert.X509Certificate) ca).getSubjectDN());
// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
// Create a TrustManager that trusts the CAs in our KeyStore
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
// Create an SSLContext that uses our TrustManager
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);
// Create all-trusting host name verifier
// to avoid the following :
// java.security.cert.CertificateException: No name matching
// This is because Java by default verifies that the certificate CN (Common Name) is
// the same as host name in the URL. If they are not, the web service client fails.
HostnameVerifier allHostsValid = new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
};
// Install it
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
// Tell the URLConnection to use a SocketFactory from our SSLContext
URL url = new URL(urlString);
HttpsURLConnection urlConnection = null;
urlConnection = (HttpsURLConnection)url.openConnection();
urlConnection.setSSLSocketFactory(sslContext.getSocketFactory());
return urlConnection;
}
catch (Exception ex)
{
Log.e("NetworkManager", "Failed to establish SSL connection to server: " + ex.toString());
return null;
}
}
/**
* Represents an asynchronous login/registration task used to authenticate
* the user.
*/
public class POSTTask extends AsyncTask<POSTRequest, Void, StringBuilder>
{
POSTTask()
{
}
@Override
protected void onPreExecute() {}
@Override
protected StringBuilder doInBackground(POSTRequest... params)
{
OutputStream os = null;
try {
HttpsURLConnection urlConnection = setUpHttpsConnection(params[0].url);
//Sets the maximum time to wait for an input stream read to complete before giving up.
urlConnection.setReadTimeout(30000);
//Sets the maximum time in milliseconds to wait while connecting.
urlConnection.setConnectTimeout(20000);
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params[0].nameValuePairs);
os = urlConnection.getOutputStream();
formEntity.writeTo(os);
InputStream in = urlConnection.getInputStream();
StringBuilder ret = inputStreamToString(in);
return ret;
} catch (IOException e) {
Log.i("NetworkError", e.toString());
} catch (Exception e) {
} finally {
if (os != null) {
try {
os.close();
} catch (IOException ex) {
}
}
}
return null;
}
@Override
protected void onPostExecute(StringBuilder result) {
}
@Override
protected void onCancelled() {
}
}
最佳答案
如果我正确理解你关于“all trusting”的想法,也就是你代码中的hostname verifier,你可以引用以下内容:
假设您的服务器应用托管在 IIS 中,IIS 中有一个服务器证书,例如,"Issued to"
是 "localhost"
。然后,在验证方法中,您可以验证 "localhost"
。
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
HostnameVerifier hv =
HttpsURLConnection.getDefaultHostnameVerifier();
return hv.verify("localhost", session);
}
};
关于android - 自签名证书 - 未找到信任 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32304014/
当用户将鼠标悬停在 anchor 上时,除了悬停的 anchor 之外的每个 anchor 都会改变颜色。假设有 link1 link2 link3。如果用户将鼠标悬停在 link1 上,我希望更改
'; echo "".$value['eventsearchresultwhat'].""; echo "\n"; echo $value['eventsearchr
我有几个多列下拉菜单作为我的主导航的一部分。由于某种原因,两个多列下拉菜单中每个链接周围的 anchor 命中区域位于链接本身上方。我该如何修复它,使点击区域像普通 anchor 一样以链接文本为中心
我正在 android studio 中创建内部构建 AR 导航。我正在寻找一种方法将 anchor 与其他 anchor 或 anchor 节点/节点“连接”起来。我不确定使用哪一个。基于我将强制用
我有很多 anchor 可以在 doxygen 中描绘,例如 \anchor pic_foo \image html foo.gif "My Caption" \anchor pic_bar \ima
我有 3 个 anchor block ,当我加载页面时,我需要 3 个不同的随机 background-colors。然而,只有第一个 anchor 随机改变 background-color。这是
我有一个带有 float 顶部导航栏的网页。我想将 anchor 链接添加到目录以快速定向到页面上的其他位置。 #top { display: flex; z-index: 100; ba
我无法理解为什么将 text-align 属性设置为 right 值,将 span 标签移出 anchor 标签区域(溢出)? HTML文件的结构 a.btn{ border: 1px solid
我需要使用 Jquery 将类“Selected”添加到一个 anchor ,捕获它的唯一方法是通过其属性“active=true”。然后,当用户选择其他 anchor 时,新 anchor 将被设置
我在 hubspot 上有一个基于搜索词查询动态生成的搜索列表页面。 HTML结构如下: a class="hs-search-results__title" href="www.somethi
标题隐晦,但很难解释。使用 Firefox,前往 http://images.google.com进行随机搜索,然后将第一个搜索结果拖到地址栏。您会看到它转到具有图像 anchor 的页面。然后单击“
我正在为 vue-router 不滚动/导航到 anchor 标签(例如:#anchor)而苦苦挣扎。我在 Stack Overflow 上阅读了各种解决方案,但到目前为止都没有奏效。 请在下面找到我
我正在制作 AR 应用程序。要求很简单。在“配置阶段”,我启动应用程序并将一个立方体放在房间的地板上,然后关闭它。稍后当我再次启动应用程序时,当相机面向同一楼层时,立方体应该留在那里。根据我发现的信息
我正在 WordPress 中通过简码展示一组产品。显示屏上有图像和按钮。 问题:只有照片包含指向单个产品页面的链接。关联的按钮没有指向单个产品页面的链接。 这是当前代码:
我正在尝试做一件奇怪的事情。我将解释一般情况,然后按照我想要执行的步骤进行。我有一个 内的项目列表。当我按下一个元素时,必须打开一个模式。在这个模态中,有与 相同的元素。但详细解释了。当模式打开时
尝试从 anchor 克隆文本并将其设置为同一 anchor 的标题属性,但不了解如何使用正确语法的逻辑。 $(document).ready(function () { $('.desc a').c
--- #File A - Lunch: &01 - Chicken - Rice - Sallad ... --- #File B - Mon
我的用例是: 点击屏幕并将“点”保存为起始 anchor 第二次点击屏幕并将“点”保存为结束 anchor 按下按钮将对象从开始 anchor 移动到结束 anchor 我已经构建了自己的节点,该节点
我的菜单和基于#anchors 的导航有问题。我正在使用 JS 函数在滚动时将菜单置于顶部(#stickyheader)。我的菜单上有一个 div,滚动时会消失(#unstickyheader)。该
我目前有这个基础页面淡入淡出脚本函数,当前针对所有 anchor ('a'),但不确定如何准确排除具有类(.noFade)的某个 anchor 淡出。 JS: $(document).ready(fu
我是一名优秀的程序员,十分优秀!