HttpClient version for testing
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId> httpclient</artifactid> <version>4.1.2</version> <scope>test</scope>< /dependency>
1. Transfer key value pairs
Http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient
2. Sending HTTPS requests
Http://javaskeleton.blogspot.it/2010/07/avoiding-peer-not-authenticated-with.html
Final Test code:
public class Logintest {@Test public void Testhttppost () throws Exception {HttpClient client = new Defaulth Ttpclient (); Client = webclientdevwrapper.wrapclient (client); HttpPost post = new HttpPost ("Https://localhost:8443/login");//stringentity entity = new Stringentity ("[email  ;p rotected]&pwd=111&type=x ");//post.setentity (entity); list<namevaluepair> namevaluepairs = new arraylist<namevaluepair> (2); Namevaluepairs.add (New Basicnamevaluepair ("User", "[email protected]")); Namevaluepairs.add (New Basicnamevaluepair ("pwd", "111")); Namevaluepairs.add (New Basicnamevaluepair ("type", "X")); Post.setentity (New Urlencodedformentity (Namevaluepairs)); HttpResponse res = Client.execute (POST); System.out.println (Res.getstatusline ()); BufferedReader br = new BufferedReader (New InputStreamReader (Res.getentity (). GetContent ())); String line = Br.readline (); WhIle (line! = null) {System.out.println (line); line = Br.readline (); } client.getconnectionmanager (). Shutdown (); }}
tool class:
/*this code is public domain:you be free to use, link and/or modify it on any it's want, for all purposes including C Ommercial applications. */public class Webclientdevwrapper {public static HttpClient wrapclient (HttpClient base) throws Exception {SSL Context CTX = sslcontext.getinstance ("TLS"); X509trustmanager TM = new X509trustmanager () {@Override public void checkclienttrusted (Java.securit Y.cert.x509certificate[] X509Certificates, String s) throws java.security.cert.CertificateException {} @Override public void checkservertrusted (java.security.cert.x509certificate[] x509certificates, String s) t Hrows java.security.cert.CertificateException {} @Override public java.security.cert.X50 9certificate[] Getacceptedissuers () {return new java.security.cert.x509certificate[0]; } }; Ctx.init (NULL, new TRUSTMANAGER[]{TM}, NULL); SslsocKetfactory SSF = new Sslsocketfactory (CTX); Ssf.sethostnameverifier (Sslsocketfactory.allow_all_hostname_verifier); Clientconnectionmanager CCM = Base.getconnectionmanager (); schemeregistry sr = Ccm.getschemeregistry (); Sr.register (New Scheme ("https", SSF, 443)); return new Defaulthttpclient (CCM, Base.getparams ()); }}
To test using the command line:
Curl-k-x POST https://localhost:8443/login--data "[Email protected]&pwd=111&type=x"
To study http://my.oschina.net/wenziqiu/blog/339630, looks more simple appearance.
Apache HttpClient Post data (HTTPS)