ใช้ httpclient เพื่อเรียกที่อยู่ URL https โดยไม่มีใบรับรองและส่งสตรีมไบต์
คัดลอกรหัสรหัสดังต่อไปนี้:
แพ็คเกจ com.paic.hmreport.metaQ;
นำเข้า java.io.BufferedInputStream;
นำเข้า java.io.BufferedReader;
นำเข้า java.io.ByteArrayInputStream;
นำเข้า java.io.FileOutputStream;
นำเข้า java.io.IOException;
นำเข้า java.io.InputStream;
นำเข้า java.io.InputStreamReader;
นำเข้า java.io.OutputStream;
นำเข้า java.net.URL;
นำเข้า java.security.KeyManagementException;
นำเข้า java.security.KeyStore;
นำเข้า java.security.NoSuchAlgorithmException;
นำเข้า javax.net.ssl.HostnameVerifier;
นำเข้า javax.net.ssl.HttpsURLConnection;
นำเข้า javax.net.ssl.KeyManagerFactory;
นำเข้า javax.net.ssl.SSLContext;
นำเข้า javax.net.ssl.SSLSession;
นำเข้า javax.net.ssl.TrustManagerFactory;
นำเข้า java.security.cert.CertificateException;
นำเข้า java.security.cert.X509Certificate;
นำเข้า javax.net.ssl.TrustManager;
นำเข้า javax.net.ssl.X509TrustManager;
นำเข้า org.apache.http.HttpEntity;
นำเข้า org.apache.http.HttpResponse;
นำเข้า org.apache.http.client.ClientProtocolException;
นำเข้า org.apache.http.client.methods.HttpPost;
นำเข้า org.apache.http.conn.scheme.Scheme;
นำเข้า org.apache.http.conn.ssl.SSLSocketFactory;
นำเข้า org.apache.http.entity.BufferedHttpEntity;
นำเข้า org.apache.http.entity.InputStreamEntity;
นำเข้า org.apache.http.entity.StringEntity;
นำเข้า org.apache.http.impl.client.DefaultHttpClient;
นำเข้า org.apache.commons.logging.Log;
นำเข้า org.apache.commons.logging.LogFactory;
HttpsClient คลาสสาธารณะ {
บันทึกบันทึกคงที่สาธารณะ = LogFactory.getLog (HttpsClient.class);
/* สำหรับ windows */
/* สำหรับลินุกซ์ */
-
* สตริงคงที่ส่วนตัว CLIENT_CERT_PWD = "123456"; สตริงคงที่ส่วนตัว
* TRUST_CERT_PATH=
* "/wls/bis_emulator/apps/emulator/config/cert/BIS_FRONT_SERVER_STG_BY_ZXC.jks"
* ; สตริงคงที่ส่วนตัว TRUST_CERT_PWD = "123456";
* ลูกค้า_cert_path=
* "/wls/bis_emulator/apps/emulator/config/cert/EXV_GROUP_EAI_B2B_ZUCHE_100.jks"
-
-
-
* @param args
* @throwsIOException
* @พ่น ClientProtocolException
* @พ่น NoSuchAlgorithmException
* @ พ่น KeyManagementException
-
โมฆะคงที่สาธารณะ main (String [] args) พ่น KeyManagementException
NoSuchAlgorithmException, ClientProtocolException, IOException {
// sendMsgOfCert("https://10.25.32.13:8007", "สวัสดีชาวโลก", "123456",
// "kserver.jks", "123456", "tclient.jks");
ส่ง(
"https://127.0.0.1/hmreport/messageChannel.ac?sign=TEZrSHZJdDNrRFNIb0M0QnJrc3VIdDBJWDRYTTVXZGJYZHlLUkpxQlp6anQyYUJITEpSVWQzOWh4b0RvOW96TGVvN2Z hWEJ3SkZvN0JIZVhhOFruaWZLY3VwbDExSjg2cjZFMFFvNHV4YktJd3E0T2RvTmVhQzV6NV hNTzJLN1NaNWpoUUhTVTR0NTNEdWFOVHpuZjh1ajA0VUhqaFBWRTJvM0s2dnEyTFVnPQ==",
"สวัสดีชาวโลก!");
-
สาธารณะสตริงคง sendMsgOfCert (สตริง urlString, สตริง requestData,
สตริง CLIENT_CERT_PWD, สตริง CLIENT_CERT_PATH,
สตริง TRUST_CERT_PWD, สตริง TRUST_CERT_PATH) {
StringBuffer sb = โมฆะ;
พยายาม {
log.info("เริ่มการเริ่มต้นไคลเอนต์ https!");
SSLContext sslContext = SSLContext.getInstance("SSL");
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
TrustManagerFactory tmf = TrustManagerFactory
.getInstance("SunX509");
KeyStore ks = KeyStore.getInstance("JKS");
ks.load (ClassLoader.getSystemResourceAsStream (CLIENT_CERT_PATH)
CLIENT_CERT_PWD.toCharArray());
kmf.init(ks, CLIENT_CERT_PWD.toCharArray());
KeyStore tks = KeyStore.getInstance("JKS");
tks.load (ClassLoader.getSystemResourceAsStream (TRUST_CERT_PATH)
TRUST_CERT_PWD.toCharArray());
tmf.init(tks);
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
HostnameVerifier hostnameVerifier = ใหม่ HostnameVerifier() {
การตรวจสอบบูลีนสาธารณะ (สตริง arg0, SSLSession arg1) {
กลับเป็นจริง;
-
-
HttpsURLConnection.setDefaultHostnameVerifier(ชื่อโฮสต์Verifier);
// URL url = URL ใหม่ ("https://172.40.1.83:8007");
URL url = URL ใหม่ (urlString);
HttpsURLConnection urlCon = (HttpsURLConnection) url
.openConnection();
urlCon.setDoOutput(จริง);
urlCon.setDoInput(จริง);
urlCon.setRequestMethod("โพสต์");
urlCon.setRequestProperty("ประเภทเนื้อหา",
"ข้อความ/xml;ชุดอักขระ=GB18030");
urlCon.setSSLSocketFactory(sslContext.getSocketFactory());
OutputStream ระบบปฏิบัติการ = urlCon.getOutputStream();
InputStream fis = ByteArrayInputStream ใหม่ (requestData
.getBytes("GB18030"));
BufferedInputStream bis = BufferedInputStream ใหม่ (fis);
ไบต์ [] ไบต์ = ไบต์ใหม่ [1024];
อินท์เลน = -1;
ในขณะที่ ((len = bis.read (ไบต์)) != -1) {
os.write(ไบต์, 0, เลน);
-
os.ฟลัช();
ทวิ.ปิด();
fis.ปิด();
os.ปิด();
InputStream คือ = urlCon.getInputStream();
BufferedReader br = ใหม่ BufferedReader (InputStreamReader ใหม่ (คือ
"GB18030"));
// DataInputStream indata = DataInputStream ใหม่ (คือ);
// String ret = "";
// สตริง str_return = "";
// ในขณะที่ (ret != null) {
// ret = indata.readLine();
// if (ret != null && !ret.trim().equals("")) {
// str_return = str_return
// + สตริงใหม่ (ret.getBytes ("ISO-8859-1"), "GBK");
-
-
// System.out.println("str_return:" + str_return);
// System.out.println("br.readLine():"+new
// String(br.readLine().getBytes("GBK"), "GBK");
sb = StringBuffer ใหม่ ();
เส้นสาย;
ในขณะที่ ((line = br.readLine()) != null) {
sb.ผนวก(บรรทัด);
-
System.out.println("sb:" + sb);
br.ปิด();
คือ.ปิด();
urlCon.disconnect();
} จับ (ข้อยกเว้นจ) {
e.fillInStackTrace();
log.info("การเรียกลูกค้าล้มเหลว: " + e.getMessage());
โยน RuntimeException ใหม่ ("การโทร https ล้มเหลว!");
-
กลับเป็นโมฆะ;
-
โมฆะคงสาธารณะส่ง (สตริง requsetString, สตริง requestData)
พ่น NoSuchAlgorithmException, KeyManagementException,
ClientProtocolException, IOException {
// ขั้นแรกให้สร้างผู้จัดการความเชื่อถือที่ไม่สนใจ
X509TrustManager trustManager = ใหม่ X509TrustManager() {
โมฆะสาธารณะ checkClientTrusted (X509Certificate [] chain,
String authType) พ่น CertificateException {
//อย่าทำอะไรเลย..
-
โมฆะสาธารณะ checkServerTrusted (X509Certificate [] chain,
String authType) พ่น CertificateException {
//อย่าทำอะไรเลย..
-
ใบรับรอง X509 สาธารณะ [] getAcceptedIssuers () {
//อย่าทำอะไรเลย..
กลับเป็นโมฆะ;
-
-
// ตอนนี้ให้ใส่ตัวจัดการความน่าเชื่อถือลงใน SSLContext
SSLContext sslcontext = SSLContext.getInstance("SSL");
sslcontext.init (null, TrustManager ใหม่ [] { trustManager }, null);
// ใช้ SSLContext ด้านบนเพื่อสร้างโรงงานซ็อกเก็ตของคุณ
// (ฉันพบว่าการพยายามขยายโรงงานเป็นเรื่องยากเล็กน้อยเนื่องจาก
// เรียก createSocket โดยไม่มีข้อโต้แย้ง ซึ่งเป็นวิธีการที่ไม่มี
// มีทุกที่ที่หาเจอ แต่เฮ้โฮ)
SSLSocketFactory sf = SSLSocketFactory ใหม่ (sslcontext);
sf.setHostnameVerifier (SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
DefaultHttpClient httpclient = DefaultHttpClient ใหม่();
httpclient.getConnectionManager().getSchemeRegistry().register(
โครงการใหม่("https", sf, 443));
// คำขอสตริง = "https://180.168.35.140/api/vm.list";
HttpPost httpPost = HttpPost ใหม่ (requsetString);
ผลลัพธ์สตริง = "";
//ดำเนินการร้องขอ HTTP
httpPost.setHeader("การอนุญาต", "พื้นฐาน"
+ "dGNsb3VkYWRtaW46dGNsb3VkMTIz");
httpPost.setHeader("ประเภทเนื้อหา", "application/xml");
StringEntity reqEntity;
// สรุปพารามิเตอร์คำขอลงใน HttpEntity
reqEntity = StringEntity ใหม่ (requestData);
BufferedHttpEntity bhe = ใหม่ BufferedHttpEntity(reqEntity);
httpPost.setEntity(บเฮ);
การตอบสนอง HttpResponse = httpclient.execute (httpPost);
HttpEntity resEntity = response.getEntity();
เครื่องอ่าน InputStreamReader = InputStreamReader ใหม่ (resEntity.getContent());
ถ่าน [] บัฟ = ถ่านใหม่ [1024];
ความยาว int = 0;
ในขณะที่ ((length = reader.read(buff)) != -1) {
ผลลัพธ์ += สตริงใหม่ (บัฟ, 0, ความยาว);
-
httpclient.getConnectionManager().ปิดเครื่อง();
System.out.println(">>>:" + ผลลัพธ์);
-
การทดสอบโมฆะคงที่สาธารณะ () {
สตริงคำ = "สวัสดี";
พยายาม {
FileOutputStream ออก = FileOutputStream ใหม่ ("D:/file.txt");
ออก.write(words.getBytes());
ออก.ล้าง();
ออก.ปิด();
} จับ (ข้อยกเว้นจ) {
// TODO บล็อก catch ที่สร้างขึ้นอัตโนมัติ
e.printStackTrace();
-
-
-