HttpsURLConnection_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > HttpsURLConnection

HttpsURLConnection

 2020/4/29 12:23:00  zw7534313  程序员俱乐部  我要评论(0)
  • 摘要:importjava.net.URL;importjavax.net.ssl.HostnameVerifier;importjavax.net.ssl.HttpsURLConnection;importjavax.net.ssl.SSLSession;publicclassHttpsUrlConnectionTest{publicstaticvoidmain(Stringargs[])throwsException{//客户端密钥库StringsslKeyStorePath="E:\\tomk
  • 标签:URL HTTP
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());
}
}
上一篇: web 下一篇: 没有下一篇了!
发表评论
用户名: 匿名