Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在没有证书的情况下连接到https web服务?_Java_Android_Ssl_Certificate - Fatal编程技术网

Java 如何在没有证书的情况下连接到https web服务?

Java 如何在没有证书的情况下连接到https web服务?,java,android,ssl,certificate,Java,Android,Ssl,Certificate,我需要为我的客户创建应用程序,应用程序的一部分使用https连接: public static void getVersions() throws IOException, ParserConfigurationException, SAXException { URL u = new URL(VERSION_URL); HttpsURLConnection c = (HttpsURLConnection) u.openConnection(); c.setReq

我需要为我的客户创建应用程序,应用程序的一部分使用https连接:

public static void getVersions() throws IOException, ParserConfigurationException, SAXException {

     URL u = new URL(VERSION_URL);
     HttpsURLConnection c = (HttpsURLConnection) u.openConnection();
     c.setRequestMethod("GET");
     c.setDoOutput(true);
     c.connect();
     InputStream in = c.getInputStream();

     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
     DocumentBuilder builder = null;
     builder = factory.newDocumentBuilder();                                   
     Document d = builder.parse(in);
}

但当我尝试使用它时,我会得到IO异常:没有可信的java证书。这是我客户的服务,一切都是合法的。如何修复此错误?

您可以将自定义服务器证书放在信任存储中,并将其作为资源包含在应用程序中

创建到该服务器的HTTPS连接时,可以指定此信任存储包含受信任的证书。这可以通过创建自己的实例来实现


另请参见:

您可以将自定义服务器证书放在信任存储中,并将其作为资源包含在应用程序中

创建到该服务器的HTTPS连接时,可以指定此信任存储包含受信任的证书。这可以通过创建自己的实例来实现


另请参见:

谢谢。你知道禁用验证的诀窍吗?是的,但它们都是胡说八道,因为你失去了使用SSL所获得的一半安全性。使用我所描述的解决方案以及包含正确dns名称的自定义证书。那个么你们就不需要禁用验证了。再次感谢你们,但你们能给我一些技巧来解决我的问题吗?但现在安全性不重要了,我需要做一个应用程序的工作原型。谢谢,谢谢。你知道禁用验证的诀窍吗?是的,但它们都是胡说八道,因为你失去了使用SSL所获得的一半安全性。使用我所描述的解决方案以及包含正确dns名称的自定义证书。那个么你们就不需要禁用验证了。再次感谢你们,但你们能给我一些技巧来解决我的问题吗?但现在安全性不重要了,我需要做一个应用程序的工作原型。非常感谢。
KeyStore trusted = KeyStore.getInstance("BKS");
InputStream in = context.getResources().openRawResource(R.raw.mystore);
trusted.load(in, "mypassword".toCharArray());
in.close();
SSLSocketFactory mySslFact = new SSLSocketFactory(trusted);