Verwenden Sie httpclient, um die https-URL-Adresse ohne Zertifikat aufzurufen und den Bytestream zu übertragen.
Kopieren Sie den Codecode wie folgt:
Paket com.paic.hmreport.metaQ;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
java.net.URL importieren;
import java.security.KeyManagementException;
java.security.KeyStore importieren;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
javax.net.ssl.SSLSession importieren;
import javax.net.ssl.TrustManagerFactory;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
öffentliche Klasse HttpsClient {
öffentliches statisches Protokoll log = LogFactory.getLog(HttpsClient.class);
/* für Windows */
/* für Linux */
/*
* private statische Zeichenfolge CLIENT_CERT_PWD="123456"; private statische Zeichenfolge
* TRUST_CERT_PATH=
* "/wls/bis_emulator/apps/emulator/config/cert/BIS_FRONT_SERVER_STG_BY_ZXC.jks"
* ; privater statischer String TRUST_CERT_PWD="123456";
* client_cert_path=
* "/wls/bis_emulator/apps/emulator/config/cert/EXV_GROUP_EAI_B2B_ZUCHE_100.jks"
* ;
*/
/**
* @param args
* @throwsIOException
* @throws ClientProtocolException
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public static void main(String[] args) löst eine KeyManagementException aus,
NoSuchAlgorithmException, ClientProtocolException, IOException {
// sendMsgOfCert("https://10.25.32.13:8007", "hello world", "123456",
// „kserver.jks“, „123456“, „tclient.jks“);
schicken(
„https://127.0.0.1/hmreport/messageChannel.ac?sign=TEZrSHZJdDNrRFNIb0M0QnJrc3VIdDBJWDRYTTVXZGJYZHlLUkpxQlp6anQyYUJITEpSVWQzOWh4b0RvOW96TGVvN2Z hWEJ3SkZvN0JIZVhhOFRuaWZLY3VwbDExSjg2cjZFMFFvNHV4YktJd3E0T2RvTmVhQzV6NV hNTzJLN1NaNWpoUUhTVTR0NTNEdWFOVHpuZjh1ajA0VUhqaFBWRTJvM0s2dnEyTFVnPQ==",
„Hallo Welt!“);
}
öffentlicher statischer String sendMsgOfCert(String urlString, String requestData,
Zeichenfolge CLIENT_CERT_PWD, Zeichenfolge CLIENT_CERT_PATH,
String TRUST_CERT_PWD, String TRUST_CERT_PATH) {
StringBuffer sb = null;
versuchen {
log.info("Initialisierung des https-Clients starten!");
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 = new HostnameVerifier() {
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
// URL url = neue URL("https://172.40.1.83:8007");
URL url = neue URL(urlString);
HttpsURLConnection urlCon = (HttpsURLConnection) URL
.openConnection();
urlCon.setDoOutput(true);
urlCon.setDoInput(true);
urlCon.setRequestMethod("POST");
urlCon.setRequestProperty("Inhaltstyp",
"text/xml;charset=GB18030");
urlCon.setSSLSocketFactory(sslContext.getSocketFactory());
OutputStream os = urlCon.getOutputStream();
InputStream fis = new ByteArrayInputStream(requestData
.getBytes("GB18030"));
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] bytes = neues byte[1024];
int len = -1;
while ((len = bis.read(bytes)) != -1) {
os.write(bytes, 0, len);
}
os.flush();
bis.close();
fis.close();
os.close();
InputStream ist = urlCon.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is,
„GB18030“));
// DataInputStream indata = new DataInputStream(is);
// String ret = "";
// String str_return = "";
// while (ret != null) {
// ret = indata.readLine();
// if (ret != null && !ret.trim().equals("")) {
// str_return = str_return
// + new String(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 = new StringBuffer();
String-Linie;
while ((line = br.readLine()) != null) {
sb.append(line);
}
System.out.println("sb:" + sb);
br.close();
is.close();
urlCon.disconnect();
} Catch (Ausnahme e) {
e.fillInStackTrace();
log.info("Client-Aufruf fehlgeschlagen: " + e.getMessage());
throw new RuntimeException("https-Aufruf fehlgeschlagen!");
}
null zurückgeben;
}
public static void send(String requsetString, String requestData)
wirft NoSuchAlgorithmException, KeyManagementException,
ClientProtocolException, IOException {
// Erstellen Sie zunächst einen Trust-Manager, dem das egal ist.
X509TrustManager trustManager = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[]-Kette,
String authType) löst CertificateException {
// Tu nichts.
}
public void checkServerTrusted(X509Certificate[]-Kette,
String authType) löst CertificateException {
// Tu nichts.
}
public X509Certificate[] getAcceptedIssuers() {
// Tu nichts.
null zurückgeben;
}
};
// Fügen Sie nun den Trust-Manager in einen SSLContext ein.
SSLContext sslcontext = SSLContext.getInstance("SSL");
sslcontext.init(null, new TrustManager[] { trustManager }, null);
// Verwenden Sie den obigen SSLContext, um Ihre Socket-Factory zu erstellen
// (Ich fand den Versuch, die Fabrik zu erweitern, etwas schwierig, weil a
// Aufruf von createSocket ohne Argumente, eine Methode, die dies nicht tut
// existieren überall, wo ich sie finden kann, aber hey-ho).
SSLSocketFactory sf = new SSLSocketFactory(sslcontext);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getConnectionManager().getSchemeRegistry().register(
neues Schema("https", sf, 443));
// String requset = "https://180.168.35.140/api/vm.list";
HttpPost httpPost = new HttpPost(requsetString);
String result = "";
//HTTP-Anfrage ausführen
httpPost.setHeader("Authorization", "basic"
+ "dGNsb3VkYWRtaW46dGNsb3VkMTIz");
httpPost.setHeader("Content-type", "application/xml");
StringEntity reqEntity;
// Anforderungsparameter in HttpEntity kapseln
reqEntity = new StringEntity(requestData);
BufferedHttpEntity bhe = new BufferedHttpEntity(reqEntity);
httpPost.setEntity(bhe);
HttpResponse-Antwort = httpclient.execute(httpPost);
HttpEntity resEntity = Response.getEntity();
InputStreamReader reader = new InputStreamReader(resEntity.getContent());
char[] buff = new char[1024];
int Länge = 0;
while ((length = reader.read(buff)) != -1) {
result += new String(buff, 0, length);
}
httpclient.getConnectionManager().shutdown();
System.out.println(">>>:" + result);
}
öffentlicher statischer void test() {
String-Wörter = „Hallo“;
versuchen {
FileOutputStream out = new FileOutputStream("D:/file.txt");
out.write(words.getBytes());
out.flush();
out.close();
} Catch (Ausnahme e) {
// TODO Automatisch generierter Catch-Block
e.printStackTrace();
}
}
}