Import java.io.IOException;
Import java.net.InetAddress;
Import Java.net.Socket;
Import java.net.UnknownHostException;
Import Javax.net.ssl.SSLContext;
Import Javax.net.ssl.TrustManager;
Import org.apache.commons.httpclient.ConnectTimeoutException;
Import Org.apache.commons.httpclient.HttpClientError;
Import Org.apache.commons.httpclient.params.HttpConnectionParams;
Import Org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
Import Org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
public class Mysecureprotocolsocketfactory implements Secureprotocolsocketfactory {
Private Sslcontext sslcontext = null;
/**
* Constructor for Mysecureprotocolsocketfactory.
*/
Public Mysecureprotocolsocketfactory () {
}
/**
* @return
*/
private static Sslcontext Createeasysslcontext () {
try {
Sslcontext context = sslcontext.getinstance ("SSL");
Context.init (NULL, new trustmanager[] {new Myx509trustmanager ()}, NULL);
return context;
} catch (Exception e) {
throw new Httpclienterror (e.tostring ());
}
}
/**
* @return
*/
Private Sslcontext Getsslcontext () {
if (This.sslcontext = = null) {
This.sslcontext = Createeasysslcontext ();
}
return this.sslcontext;
}
/*
* (Non-javadoc)
* @see
* Org.apache.commons.httpclient.protocol.protocolsocketfactory#createsocket (java.lang.String,
* int, java.net.InetAddress, int)
*/
Public Socket Createsocket (String host, int port, inetaddress clienthost, int clientport) throws IOException,
unknownhostexception {
Return Getsslcontext (). Getsocketfactory (). Createsocket (host, Port, ClientHost, ClientPort);
}
/*
* (Non-javadoc)
* @see
* Org.apache.commons.httpclient.protocol.protocolsocketfactory#createsocket (java.lang.String,
* int, java.net.InetAddress, int, org.apache.commons.httpclient.params.HttpConnectionParams)
*/
Public Socket Createsocket (final String host, final int port, final inetaddress localaddress, final int localport,
Final httpconnectionparams params) throws IOException, Unknownhostexception, connecttimeoutexception {
if (params = = null) {
throw new IllegalArgumentException ("Parameters May is not null");
}
int timeout = params.getconnectiontimeout ();
if (timeout = = 0) {
Return Createsocket (host, Port, localaddress, LocalPort);
} else {
Return Controllerthreadsocketfactory.createsocket (this, host, Port, localaddress, LocalPort, timeout);
}
}
/*
* (Non-javadoc)
* @see Secureprotocolsocketfactory#createsocket (java.lang.string,int)
*/
Public Socket Createsocket (String host, int port) throws IOException, Unknownhostexception {
Return Getsslcontext (). Getsocketfactory (). Createsocket (host, Port);
}
/*
* (Non-javadoc)
* @see Secureprotocolsocketfactory#createsocket (Java.net.socket,java.lang.string,int,boolean)
*/
Public socket Createsocket (socket socket, String host, int port, Boolean autoClose) throws IOException,
unknownhostexception {
Return Getsslcontext (). Getsocketfactory (). Createsocket (socket, host, port, AutoClose);
}
}
Import java.security.cert.CertificateException;
Import java.security.cert.X509Certificate;
Import Javax.net.ssl.X509TrustManager;
public class Myx509trustmanager implements X509trustmanager {
/*
* (non-javadoc)
* @see Javax.net.ssl . X509trustmanager#checkclienttrusted (java.security.cert.x509certificate[],
* java.lang.String)
*/
public void checkclienttrusted (x509certificate[] arg0, String arg1) throws Certificateexception {
}
/* * (non-javadoc)
* @see javax.net.ssl.x509trustmanager#checkservertrusted (java.security.cert.x509certificate[ ],
* java.lang.String)
*/
public void checkservertrusted (x509certificate[] arg0, String arg1) throws Certific ateexception {
}
/*
* (non-javadoc)
* @see javax.net.ssl.x509trustmanager#getacceptedissuers ()
*/
Public x509certificate[] Getacceptedissuers () {
return null;
}
}
public static void Main (string[] args) {
try {
System.out.println (Toolutils.encryptbase64 ("cb:20160803010100"));
System.out.println (TOOLUTILS.MD5 ("cb201608030101001572874614213670297489123456789"));
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Bidirectionalbean bidbean=new Bidirectionalbean ();
Bidbean.setcallernumber ("15728746142");
Bidbean.setcallednumber ("13670297489");
Bidbean.setshowcaller ("");
Bidbean.setuserdata ("123456789");
Bidbean.setsig ("3bb3b074d67ae8916cda3899bc7cc7e4");
String responsemsg = "";
Constructing HttpClient Instances
Protocolsocketfactory fcty = new Mysecureprotocolsocketfactory ();
Protocol.registerprotocol ("https", New Protocol ("https", Fcty, 443));
HttpClient http=new HttpClient ();
transcoding format for parameters
Http.getparams (). Setcontentcharset ("Utf-8");
Postmethod post=new Postmethod ("Https://192.168.1.58:8443/CtdWebCall/api/extension/call");
Post.getparams (). Setparameter (Httpmethodparams.retry_handler,new Defaulthttpmethodretryhandler ());
Post.getparams (). Setparameter (Httpmethodparams.so_timeout, 2000);
Post.setrequestheader ("Content-type", "application/json;charset=utf-8");
Post.setrequestheader ("Accept", "Application/json");
Post.setrequestheader ("Authorization", "y2i6mjaxnja4mdmwmtaxmda=");
Requestentity Requeste;
try {
Requeste = new Stringrequestentity (json.tojsonstring (Bidbean), "Application/json", "utf-8");
Post.setrequestentity (Requeste);
Http.executemethod (POST);
if (Post.getstatuscode () ==httpstatus.sc_ok) {//200http request returned successfully
The parameter returned by the request, the first way
Responsemsg=post.getresponsebodyasstring (). Trim ();
System.out.println (RESPONSEMSG);
}
} catch (Unsupportedencodingexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (HttpException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
if (post!=null) {
Close the request connection
Post.releaseconnection ();
}
}
}
Java httpclient Skipping certificate validation