Using httpclient does not require a certificate to invoke HTTPS's URL address to transmit a byte stream.
Package 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;
Import Java.net.URL;
Import java.security.KeyManagementException;
Import Java.security.KeyStore;
Import java.security.NoSuchAlgorithmException;
Import Javax.net.ssl.HostnameVerifier;
Import javax.net.ssl.HttpsURLConnection;
Import Javax.net.ssl.KeyManagerFactory;
Import Javax.net.ssl.SSLContext;
Import javax.net.ssl.SSLSession;
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;
public class Httpsclient {
public static log = Logfactory.getlog (Httpsclient.class);
/* for Windows */
/* for Linux */
/*
* private static String client_cert_pwd= "123456"; private static STRING
* trust_cert_path=
* "/wls/bis_emulator/apps/emulator/config/cert/bis_front_server_stg_by_ Zxc.jks "
*; private static string trust_cert_pwd=" 123456 "; private static string
* client_cert_path=
* "/wls/bis_emulator/apps/emulator/config/cert/exv_group_eai_b2b_zuche_100.jks"
*;
*/
/**
* @param args
* @throws ioexception
* @throws clientprotocolexception
& nbsp * @throws nosuchalgorithmexception
* @throws keymanagementexception
*/
public static void Main (string[] args) throws Keymanagementexception,
nosuchalgorithmexception, Clientprotocolexception, IOException {
//sendmsgofcert ("https://10.25.32.13:8007", "Hello World", " 123456 ",
//" Kserver.jks "," 123456 "," Tclient.jks ");
send (
https://127.0.0.1/hmreport/messageChannel.ac?sign= Tezrshzjddnrrfnib0m0qnjrc3viddbjwdryttvxzgjyzhllukpxqlp6anqyyujitepsvwqzowh4b0rvow96tgvvn2zhwej3skzvn0jizvhhofruawzly3vwb dexsjg2cjzfmffvnhv4yktjd3e0t2rvtmvhqzv6nvhntzjln1nanwpouuhtvtr0ntnedwfovhpuzjh1aja0vuhqafbwrtjvm0s2dneytfvnpq== ",
" helloworld! ");
}
public static string Sendmsgofcert (String urlstring, String requestdata,
String client_cert_pwd, String Client_cert_path,
String trust_cert_pwd, String trust_cert_path) {
StringBuffer SB = null;
try {
Log.info ("Start initializing HTTPS client!") ");
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 = new URL ("https://172.40.1.83:8007");
url url = new URL (urlstring);
httpsurlconnection Urlcon = (httpsurlconnection) URL
. OpenConnection ();
urlcon.setdooutput (TRUE);
urlcon.setdoinput (TRUE);
urlcon.setrequestmethod ("POST");
urlcon.setrequestproperty ("Content-type",
"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 = new 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 is = Urlcon.getinputstream ();
BufferedReader br = new BufferedReader (The 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 Line;
while (line = Br.readline ())!= null) {
Sb.append (line);
}
System.out.println ("SB:" + SB);
Br.close ();
Is.close ();
Urlcon.disconnect ();
catch (Exception e) {
E.fillinstacktrace ();
Log.info ("Client call failed:" + e.getmessage ());
throw new RuntimeException ("HTTPS call failed!") ");
}
return null;
}
public static void Send (String requsetstring, String requestdata)
Throws NoSuchAlgorithmException, Keymanagementexception,
Clientprotocolexception, IOException {
This is won ' t care, a trust manager.
X509trustmanager TrustManager = new X509trustmanager () {
public void checkclienttrusted (x509certificate[] chain,
String authtype) throws Certificateexception {
Don ' t do anything.
}
public void checkservertrusted (x509certificate[] chain,
String authtype) throws Certificateexception {
Don ' t do anything.
}
Public x509certificate[] Getacceptedissuers () {
Don ' t do anything.
return null;
}
};
Now put on the trust manager in an sslcontext.
Sslcontext Sslcontext = sslcontext.getinstance ("SSL");
Sslcontext.init (NULL, new trustmanager[] {TrustManager}, NULL);
Use the above Sslcontext to create your socket factory
(I found trying to extend the factory a bit difficult due to a
Call to Createsocket and no arguments, a method which doesn ' t
exist anywhere I can find, but hey-ho).
Sslsocketfactory SF = new Sslsocketfactory (sslcontext);
Sf.sethostnameverifier (Sslsocketfactory.allow_all_hostname_verifier);
Defaulthttpclient httpclient = new Defaulthttpclient ();
Httpclient.getconnectionmanager (). Getschemeregistry (). Register (
New Scheme ("https", SF, 443));
String requset = "Https://180.168.35.140/api/vm.list";
HttpPost HttpPost = new HttpPost (requsetstring);
String result = "";
Execute HTTP Request
Httppost.setheader ("Authorization", "basic"
+ "Dgnsb3vkywrtaw46dgnsb3vkmtiz");
Httppost.setheader ("Content-type", "Application/xml");
Stringentity reqentity;
Encapsulate request parameters into httpentity
reqentity = new Stringentity (requestdata);
Bufferedhttpentity BHE = new bufferedhttpentity (reqentity);
Httppost.setentity (BHE);
HttpResponse response = Httpclient.execute (HttpPost);
Httpentity resentity = response.getentity ();
InputStreamReader reader = new InputStreamReader (Resentity.getcontent ());
char[] buff = new char[1024];
int length = 0;
while (length = Reader.read (Buff))!=-1) {
Result + = new String (buff, 0, length);
}
Httpclient.getconnectionmanager (). Shutdown ();
System.out.println (">>>:" + result);
}
public static void Test () {
String words = "Hello";
try {
FileOutputStream out = new FileOutputStream ("D:/file.txt");
Out.write (Words.getbytes ());
Out.flush ();
Out.close ();
catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}