HttpsURLConnection
程序员文章站
2022-05-30 21:46:35
...
import java.net.URL;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
public class HttpsUrlConnectionTest {
public static void main(String args[])throws Exception{
// 客户端密钥库
String sslKeyStorePath="E:\\tomk.keystore";
String sslKeyStorePassword= "136";
String sslKeyStoreType="JKS"; // 密钥库类型,有JKS nPKCS12等;
// 客户端信任的证书
String sslTrustStore="E:\\tomk.keystore";
String sslTrustStorePassword="136";
String httpsUrlConnectionUrl = "https://localhost:8443/httpsRequest";
System.setProperty("javax.net.ssl.keyStore", sslKeyStorePath);
System.setProperty("javax.net.ssl.keyStorePassword",
sslKeyStorePassword);
System.setProperty("javax.net.ssl.keyStoreType", sslKeyStoreType);
// 设置系统参数
System.setProperty("javax.net.ssl.trustStore", sslTrustStore);
System.setProperty("javax.net.ssl.trustStorePassword",
sslTrustStorePassword);
System.setProperty("java.protocol.handler.pkgs", "sun.net.www.protocol");
URL url = new URL(httpsUrlConnectionUrl);
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
HttpsURLConnection connection = (HttpsURLConnection) url
.openConnection();
connection.setRequestProperty("Content-Type", "text/xml");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setReadTimeout(30000);
String user="abin";
String pwd="abing";
String request="user="+user+"&pwd="+pwd;
OutputStream out = connection.getOutputStream();
out.write(request.getBytes());
out.flush();
out.close();
//接收请求的返回值
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer stb = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
stb.append(line);
}
Integer statusCode = connection.getResponseCode();
System.out.println("返回状态码:" + statusCode);
reader.close();
connection.disconnect();
System.out.println(stb.toString());
}
}
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
public class HttpsUrlConnectionTest {
public static void main(String args[])throws Exception{
// 客户端密钥库
String sslKeyStorePath="E:\\tomk.keystore";
String sslKeyStorePassword= "136";
String sslKeyStoreType="JKS"; // 密钥库类型,有JKS nPKCS12等;
// 客户端信任的证书
String sslTrustStore="E:\\tomk.keystore";
String sslTrustStorePassword="136";
String httpsUrlConnectionUrl = "https://localhost:8443/httpsRequest";
System.setProperty("javax.net.ssl.keyStore", sslKeyStorePath);
System.setProperty("javax.net.ssl.keyStorePassword",
sslKeyStorePassword);
System.setProperty("javax.net.ssl.keyStoreType", sslKeyStoreType);
// 设置系统参数
System.setProperty("javax.net.ssl.trustStore", sslTrustStore);
System.setProperty("javax.net.ssl.trustStorePassword",
sslTrustStorePassword);
System.setProperty("java.protocol.handler.pkgs", "sun.net.www.protocol");
URL url = new URL(httpsUrlConnectionUrl);
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
HttpsURLConnection connection = (HttpsURLConnection) url
.openConnection();
connection.setRequestProperty("Content-Type", "text/xml");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setReadTimeout(30000);
String user="abin";
String pwd="abing";
String request="user="+user+"&pwd="+pwd;
OutputStream out = connection.getOutputStream();
out.write(request.getBytes());
out.flush();
out.close();
//接收请求的返回值
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer stb = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
stb.append(line);
}
Integer statusCode = connection.getResponseCode();
System.out.println("返回状态码:" + statusCode);
reader.close();
connection.disconnect();
System.out.println(stb.toString());
}
}
上一篇: GZIPOutputStream压缩文件
下一篇: Java加密和数字签名