Java中请求HTTPS加密的源代码
2007-04-18 14:26
316 查看
import java.io.*;
import java.net.*;
import java.security.*;
import java.security.cert.*;
import java.util.*;
import javax.net.ssl.*;
public class HttpsTest {
// We would never hardcode this literal in a real system,
// this is only for this article.
private String url = "https://localhost/";
// Create an anonymous class to trust all certificates.
// This is bad style, you should create a separate class.
private X509TrustManager xtm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
public void checkServerTrusted(X509Certificate[] chain, String authType) {
System.out.println("cert: " + chain[0].toString() + ", authType: " + authType);
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
// Create an class to trust all hosts
private HostnameVerifier hnv = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
System.out.println("hostname: " + hostname);
return true;
}
};
// In this function we configure our system with a less stringent
// hostname verifier and X509 trust manager. This code is
// executed once, and calls the static methods of HttpsURLConnection
public HttpsTest() {
// Initialize the TLS SSLContext with
// our TrustManager
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("TLS");
X509TrustManager[] xtmArray = new X509TrustManager[] { xtm };
sslContext.init(null, xtmArray, new java.security.SecureRandom());
} catch(GeneralSecurityException gse) {
// Print out some error message and deal with this exception
}
// Set the default SocketFactory and HostnameVerifier
// for javax.net.ssl.HttpsURLConnection
if(sslContext != null) {
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
}
HttpsURLConnection.setDefaultHostnameVerifier(hnv);
}
// This function is called periodically, the important thing
// to note here is that there is no special code that needs to
// be added to deal with a "HTTPS" URL. All of the trust
// management, verification, is handled by the HttpsURLConnection.
public void run() {
try {
URLConnection urlCon = (new URL(url)).openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
String line;
while((line = in.readLine()) != null) {
System.out.println(line);
}
// Whatever we want to do with these quotes
} catch(MalformedURLException mue) {
mue.printStackTrace();
} catch(IOException ioe) {
ioe.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
HttpsTest httpsTest = new HttpsTest();
httpsTest.run();
}
}
import java.net.*;
import java.security.*;
import java.security.cert.*;
import java.util.*;
import javax.net.ssl.*;
public class HttpsTest {
// We would never hardcode this literal in a real system,
// this is only for this article.
private String url = "https://localhost/";
// Create an anonymous class to trust all certificates.
// This is bad style, you should create a separate class.
private X509TrustManager xtm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
public void checkServerTrusted(X509Certificate[] chain, String authType) {
System.out.println("cert: " + chain[0].toString() + ", authType: " + authType);
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
// Create an class to trust all hosts
private HostnameVerifier hnv = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
System.out.println("hostname: " + hostname);
return true;
}
};
// In this function we configure our system with a less stringent
// hostname verifier and X509 trust manager. This code is
// executed once, and calls the static methods of HttpsURLConnection
public HttpsTest() {
// Initialize the TLS SSLContext with
// our TrustManager
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("TLS");
X509TrustManager[] xtmArray = new X509TrustManager[] { xtm };
sslContext.init(null, xtmArray, new java.security.SecureRandom());
} catch(GeneralSecurityException gse) {
// Print out some error message and deal with this exception
}
// Set the default SocketFactory and HostnameVerifier
// for javax.net.ssl.HttpsURLConnection
if(sslContext != null) {
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
}
HttpsURLConnection.setDefaultHostnameVerifier(hnv);
}
// This function is called periodically, the important thing
// to note here is that there is no special code that needs to
// be added to deal with a "HTTPS" URL. All of the trust
// management, verification, is handled by the HttpsURLConnection.
public void run() {
try {
URLConnection urlCon = (new URL(url)).openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
String line;
while((line = in.readLine()) != null) {
System.out.println(line);
}
// Whatever we want to do with these quotes
} catch(MalformedURLException mue) {
mue.printStackTrace();
} catch(IOException ioe) {
ioe.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
HttpsTest httpsTest = new HttpsTest();
httpsTest.run();
}
}
相关文章推荐
- Java中请求HTTPS加密的源代码
- Java请求Https SSL加密代码
- JAVA利用HttpClient进行POST和GET请求(HTTPS)
- 代码加密:加密Java源代码,保护自己的版权!
- java实现https ssl请求url
- JAVA利用HttpClient进行POST请求(HTTPS)
- JAVA利用HttpClient进行POST请求(HTTPS)
- java安全(加密、数字签名、数字证书、HTTPS协议)
- 如何在java中发起http和https请求 配置信任
- java(13)--配置https加密连接器并生成数字证书
- php的curl获取https加密协议请求返回json数据进行信息获取
- 运用加密技术保护Java源代码
- java访问https请求
- JAVA HTTPS请求 HttpsURLConnection
- JAVA发送http、https请求
- Java https请求 HttpsURLConnection
- URL请求对参数前端JS加密,后台JAVA解密
- java非对称加密的源代码(RSA)
- php的curl获取https加密协议请求返回json数据进行信息获取
- java HttpsURLConnection请求报错