我是靠谱客的博主 魁梧小伙,最近开发中收集的这篇文章主要介绍java post请求跳过证书验证,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述




import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.commons.lang.StringUtils;

public class GetSessionIDUtil {
public static String getSessionId(String postData)
throws NoSuchAlgorithmException, KeyManagementException {
String sessionId = "";
String createSessionURL = "https://hlixgr244.apac.nsroot.net/CONAV/CBOLSessionManagement/createsession";
try {
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}

public void checkClientTrusted(
java.security.cert.X509Certificate[] certs,
String authType) {
}

public void checkServerTrusted(
java.security.cert.X509Certificate[] certs,
String authType) {
}
} };
HttpsURLConnection
.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String string, SSLSession ssls) {
return true;
}
});
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection
.setDefaultSSLSocketFactory(sc.getSocketFactory());

URL url = new URL(createSessionURL);
HttpURLConnection connect = (HttpURLConnection) url .openConnection();
connect.setDoOutput(true);
DataOutputStream out = new DataOutputStream(connect .getOutputStream());
out.writeBytes(postData);
out.flush();
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader( connect.getInputStream()));

String line;

while ((line = in.readLine()) != null) {
// sessionId += "n" + line;
System.out.println("page info ===> " + line);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(sessionId + "s");
return sessionId;
}

public static void main(String[] as) throws KeyManagementException,
NoSuchAlgorithmException, UnsupportedEncodingException {
String GRBNumber = "004195137";
String PageID = "HomePage";
String Initial_Request_timestamp = "1317023781100";
String ICHANNEL_ID = "Default";
String DIGITAL_SIGNATURE = "X6FYQO2Gy2rJ1ir5dVUcyvRFqMoGDYzymy3EfzE0Hv/09A+zlxtKGk4mJRvRhJH0+GtSqQUhzSHFauQeW3ZQ9YqT673JmDapYlh3X0sVdPCcUJLqKIHRiqz91HRTxTcm7y7R+J66+awiKHOhTH9hHxzWf6W5n8RvI2N9cfZDJ9E=";
DIGITAL_SIGNATURE=StringUtils.replace(DIGITAL_SIGNATURE, "+", "%2B");
String postData ="GRBNumber=" + GRBNumber + "&PageID=" + PageID
+ "&Initial_Request_timestamp=" + Initial_Request_timestamp
+ "&ICHANNEL_ID=" + ICHANNEL_ID + "&DIGITAL_SIGNATURE="
+ DIGITAL_SIGNATURE;

// postData = URLEncoder.encode(postData);
System.out.println(postData);
getSessionId(postData);


}
}

最后

以上就是魁梧小伙为你收集整理的java post请求跳过证书验证的全部内容,希望文章能够帮你解决java post请求跳过证书验证所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部