`
vtyi
  • 浏览: 83470 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

URLConnection连https报证书错误的解决方法

阅读更多

导入证书

在DOS窗口,切入证书路径下执行:
keytool -import -keystore "%JAVA_HOME%/jre/lib/security/cacerts" -storepass changeit -keypass changeit -alias bocommca -file test_root.cer

"test_root.cer"为证书名.

 

删除证书

keytool -delete -alias bocommca -keystore "%JAVA_HOME%/jre/lib/security/cacerts" -storepass changeit

 

 

URLConnection and https

with a java.net.URLConnection i can connect to any http server. it’s also possible to connect to an https server. if i connect to a https server with a browser i might get a message that the certificate is not trusted. i am prompted to examine the certificate and mark it as a trusted certificate. after that i can connect without any problems. the same must be done if i try to connect with an URLConnection. if we try to connect to an https server via URLConnection and the certificate is not trusted a javax.net.ssl.SSLHandshakeException is thrown with the message “PKIX path building failed”… at least for the sun jvm version 1.5.

add certificate to a KeyStore

first we need to download the certificate from the webserver. this can be done with firefox. if you accepted the servers certificate you can save the certificate by selecting: Edit->Preferences->Advanced->Encryption->View Certificates->Your Certificates here you need to select the certificate and then click on export. save it somewhere on your harddisk. with this certificate java cannot work directly… actually it can but it’s easier to transform it into a KeyStore file. with the command keytool -import -alias aliasOfCertifiate -file certificateFile.cer\ -keystore myKeystore the keytool program is distributed with a jdk. with the command we add the certificate certificateFile.cer as a trusted certificate to the keystore file named myKeystore. the tool prompts for a password. this password is used to encrypt the keystore file.
instead of adding the certificate to myKeystore we could also add it to the default keystore of the jvm. this is done with: keytool -import -alias aliasOfCertifiate -file certificateFile.cer\ -keystore $JAVA_HOME/lib/security/cacerts with the password “changeit”. this uses root privileges and it is the default setting of all java programs. it’s a bit like pollution of the “global” environment and it’s better to avoid this.

use that keystore

if i have an URLConnection with https as a protocol it’s an instance of HttpsURLConnection and i can simply cast to it. HttpsURLConnection has a method setSSLSocketFactory. this socketFactary can be configured to accept certain certificates or not. a socketFactory which accepts certificates in myKeystore can be created with the following code: InputStream in = new FileInputStream(new File("path/to/myKeystore")); KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); ks.load(in, "PasswordUsedWithKeytool".toCharArray()); in.close(); TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(ks); X509TrustManager defaultTrustManager = (X509TrustManager)tmf.getTrustManagers()[0]; SSLContext context = SSLContext.getInstance(”TLS”); context.init(null, new TrustManager[] {defaultTrustManager}, null); SSLSocketFactory sslSocketFactory = context.getSocketFactory(); here the keystore is loaded at first. you have to provide the password you typed in during creation of the keystore file. after that a TrustManager is created via a TrustManagerFactory initialised with our KeyStore. then the SSLContext is created and initialised with the trustManager. after that a SSLSocketFactory can be created by the getSocketFactory method of the SSLContext. we can use it for our URLConnection like following: URL url = new URL("https://thesecuredomain.org"); URLConnection con = url.openConnection(); ((HttpsURLConnection) con).setSSLSocketFactory(sslSocketFactory); con.connect(); in = con.getInputStream(); ...
分享到:
评论

相关推荐

    UrlConnection连接和Socket连接的区别

    在使用`UrlConnection`时,我们通常会通过`openConnection()`方法创建连接,然后调用`setRequestProperty()`设置请求头,最后通过`getInputStream()`或`getOutputStream()`读写数据。 相比之下,`Socket`是更底层的...

    URL以及URLConnection的使用

    理解URL的各个组成部分后,我们可以通过`openConnection()`方法获取到与该URL对应的URLConnection对象,它是`java.net.URLConnection`类的实例。URLConnection是Java中连接网络资源的基础,它可以用于读取、写入或...

    IOS 程序使用urlconnection连接服务器方法

    ### IOS程序使用URLConnection连接服务器方法 #### 一、引言 在iOS开发中,应用程序往往需要与后端服务器进行通信来获取数据或提交用户操作的结果。`NSURLConnection`(现已被`URLSession`替代)是一种常用的网络...

    java中用URLConnection_类post方式提交表单

    2. 打开连接:使用openConnection()方法打开连接,该方法返回一个URLConnection对象。 3. 设置请求方法:使用setRequestMethod()方法设置请求方法为"POST"。 4. 设置请求头:使用setRequestProperty()方法设置请求...

    客户端页面截取!URLCONNECTION

    在IT行业中,客户端页面截取是一项常见的需求...虽然HttpClient在许多场景下更受欢迎,但了解和掌握URLCONNECTION的使用也能增强我们解决网络通信问题的能力。在实际开发中,选择哪种方式取决于具体的需求和项目规模。

    Android HttpURLConnection.getResponseCode()错误解决方法

    导语:个人对网络连接接触的不多,在使用时自己发现一些问题,记录一下。... 解决方法: 方法1、网页返回内容不能是空; 方法2、不要用这个接口咯。 您可能感兴趣的文章:Android使用URLConnection提交请求的实现Androi

    使用URL和URLConnection(多线程下载)

    `URL`类的`openConnection()`方法返回一个`URLConnection`实例,利用这个实例,我们能够进一步设置请求参数、读取响应头或读取/写入实体数据。 #### 二、多线程下载原理 多线程下载的基本思想是将一个大文件分割成...

    URLConnection进行网络编程

    当你创建一个`URL`对象并调用其`openConnection()`方法时,会返回一个`URLConnection`实例。这个连接对象可以用来设置请求头、发送数据以及接收响应。`URLConnection`支持多种协议,如HTTP、HTTPS等,使得它成为...

    java URL URLConnection

    通过`URL`构建连接,利用`URLConnection`进行数据交换,结合自定义工具类如`WebUrlRequestBuilder`,我们可以构建出强大且灵活的网络请求解决方案。对于任何Java开发者来说,掌握这两者是提升技能的必要步骤。

    URLConnection的使用(1)

    对于那些希望实现客户端与服务器端(如网页或Servlet等)交互的应用开发者来说,理解并掌握`URLConnection`的使用方法至关重要。 #### 一、基本概念 1. **URL (Uniform Resource Locator):** 统一资源定位符,是...

    JDK中的URLConnection参数详解

    - 当调用`URL.openConnection()`方法时,系统会根据URL的协议(如http或https)创建对应的`URLConnection`子类实例。例如,对于HTTP,返回的是`HttpURLConnection`对象。为了更好地利用特定于HTTP的功能,我们需要...

    okhttp-urlconnection:3.10.0.jar

    okhttp-urlconnection:3.10.0

    通过java.net.URLConnection发送HTTP请求

    URLConnection对象提供了设置请求属性的方法,如设置请求方法(GET、POST等)、设置请求头(如User-Agent、Content-Type等)以及设置超时等。例如,要发送POST请求并设置Content-Type为application/json,可以这样做...

    java中用URLConnection类post方式提交表单.pdf

    * 需要了解 URLConnection 类和 HttpURLConnection 对象的使用方法。 Java 中使用 URLConnection 类 POST 方式提交表单是一种常用的技术,能够实现 HTTP 请求代理,满足不同的业务需求。但是,需要掌握 Java 语言和...

    Okhttp-urlconnection

    同时使用picasso和OKHttp的时候,需要加入OKHttp-urlconnection

    java URLConnection全面解析(通俗易懂,包括cookie操作和上传文件)

    在上面的示例中,我们通过`openConnection()`方法创建了一个`URLConnection`实例,并设置了`Accept-Charset`请求头,以指定客户端期望接收的数据编码方式。如果无需发送参数或请求头,则可以简化为`new URL(url)....

    使用urlconnection下载文件或图片并保存到本地

    import java.net.URLConnection; /** * 使用URLConnection下载文件或图片并保存到本地。 * * @author 老紫竹(laozizhu.com) */ public class URLConnectionDownloader { public static void main...

    android 使用URL和URLConnection(多线程下载)_Hi Android_百度空间

    android 使用URL和URLConnection(多线程下载)_Hi Android_百度空间

    java中URL以及URLConnection类的使用

    要与一个URL建立连接,首先需要创建一个URL对象,然后调用这个对象的`openConnection()`方法来获取URLConnection对象。之后,可以使用URLConnection对象的方法来打开连接、设置请求属性以及读取响应。 #### 常用...

Global site tag (gtag.js) - Google Analytics