Apache HttpClient Post data (HTTPS)

Source: Internet
Author: User

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&nbsp        ;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.