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 commandkeytool -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`时,我们通常会通过`openConnection()`方法创建连接,然后调用`setRequestProperty()`设置请求头,最后通过`getInputStream()`或`getOutputStream()`读写数据。 相比之下,`Socket`是更底层的...
理解URL的各个组成部分后,我们可以通过`openConnection()`方法获取到与该URL对应的URLConnection对象,它是`java.net.URLConnection`类的实例。URLConnection是Java中连接网络资源的基础,它可以用于读取、写入或...
### IOS程序使用URLConnection连接服务器方法 #### 一、引言 在iOS开发中,应用程序往往需要与后端服务器进行通信来获取数据或提交用户操作的结果。`NSURLConnection`(现已被`URLSession`替代)是一种常用的网络...
2. 打开连接:使用openConnection()方法打开连接,该方法返回一个URLConnection对象。 3. 设置请求方法:使用setRequestMethod()方法设置请求方法为"POST"。 4. 设置请求头:使用setRequestProperty()方法设置请求...
在IT行业中,客户端页面截取是一项常见的需求...虽然HttpClient在许多场景下更受欢迎,但了解和掌握URLCONNECTION的使用也能增强我们解决网络通信问题的能力。在实际开发中,选择哪种方式取决于具体的需求和项目规模。
导语:个人对网络连接接触的不多,在使用时自己发现一些问题,记录一下。... 解决方法: 方法1、网页返回内容不能是空; 方法2、不要用这个接口咯。 您可能感兴趣的文章:Android使用URLConnection提交请求的实现Androi
`URL`类的`openConnection()`方法返回一个`URLConnection`实例,利用这个实例,我们能够进一步设置请求参数、读取响应头或读取/写入实体数据。 #### 二、多线程下载原理 多线程下载的基本思想是将一个大文件分割成...
当你创建一个`URL`对象并调用其`openConnection()`方法时,会返回一个`URLConnection`实例。这个连接对象可以用来设置请求头、发送数据以及接收响应。`URLConnection`支持多种协议,如HTTP、HTTPS等,使得它成为...
通过`URL`构建连接,利用`URLConnection`进行数据交换,结合自定义工具类如`WebUrlRequestBuilder`,我们可以构建出强大且灵活的网络请求解决方案。对于任何Java开发者来说,掌握这两者是提升技能的必要步骤。
对于那些希望实现客户端与服务器端(如网页或Servlet等)交互的应用开发者来说,理解并掌握`URLConnection`的使用方法至关重要。 #### 一、基本概念 1. **URL (Uniform Resource Locator):** 统一资源定位符,是...
- 当调用`URL.openConnection()`方法时,系统会根据URL的协议(如http或https)创建对应的`URLConnection`子类实例。例如,对于HTTP,返回的是`HttpURLConnection`对象。为了更好地利用特定于HTTP的功能,我们需要...
okhttp-urlconnection:3.10.0
URLConnection对象提供了设置请求属性的方法,如设置请求方法(GET、POST等)、设置请求头(如User-Agent、Content-Type等)以及设置超时等。例如,要发送POST请求并设置Content-Type为application/json,可以这样做...
* 需要了解 URLConnection 类和 HttpURLConnection 对象的使用方法。 Java 中使用 URLConnection 类 POST 方式提交表单是一种常用的技术,能够实现 HTTP 请求代理,满足不同的业务需求。但是,需要掌握 Java 语言和...
同时使用picasso和OKHttp的时候,需要加入OKHttp-urlconnection
在上面的示例中,我们通过`openConnection()`方法创建了一个`URLConnection`实例,并设置了`Accept-Charset`请求头,以指定客户端期望接收的数据编码方式。如果无需发送参数或请求头,则可以简化为`new URL(url)....
import java.net.URLConnection; /** * 使用URLConnection下载文件或图片并保存到本地。 * * @author 老紫竹(laozizhu.com) */ public class URLConnectionDownloader { public static void main...
android 使用URL和URLConnection(多线程下载)_Hi Android_百度空间
要与一个URL建立连接,首先需要创建一个URL对象,然后调用这个对象的`openConnection()`方法来获取URLConnection对象。之后,可以使用URLConnection对象的方法来打开连接、设置请求属性以及读取响应。 #### 常用...