The previous article said that HttpClient could not directly access HTTPS resources, this time to simulate the environment, and then configure the HTTPS test. In the previous article, we shared an article, tomcat configuration SSL, that I built and configured SSL in Tomcat, where you can configure HTTPS locally. I have configured it so that the effect is such a drop:
You can see that the certificate has been trusted (displays a light green small lock) and the browser can access it normally. Now let's test it in code:
- Public static void Main (string[] args) throws ParseException, IOException, Keymanagementexception, NoSuchAlgorithmException, httpprocessexception {
- String url = "Https://sso.tgb.com:8443/cas/login";
- String BODY = Send (URL, null, "Utf-8");
- SYSTEM.OUT.PRINTLN ("Transaction response result:");
- System.out.println (body);
- System.out.println ("-----------------------------------");
- }
The discovery throws an exception, I know there are two scenarios (and perhaps I do not know), here is the first scenario, but also the use of a more numerous scenarios-bypassing certificate validation. Look directly at the code:
- /**
- * Bypass Authentication
- *
- * @return
- * @throws nosuchalgorithmexception
- * @throws keymanagementexception
- */
- Public static Sslcontext Createignoreverifyssl () throws NoSuchAlgorithmException, keymanagementexception {
- Sslcontext sc = sslcontext.getinstance ("SSLv3");
- //Implement a X509trustmanager interface for bypassing validation without modifying the method inside
- X509trustmanager TrustManager = new X509trustmanager () {
- @Override
- public void checkclienttrusted (
- Java.security.cert.x509certificate[] Paramarrayofx509certificate,
- String paramstring) throws certificateexception {
- }
- @Override
- public void checkservertrusted (
- Java.security.cert.x509certificate[] Paramarrayofx509certificate,
- String paramstring) throws certificateexception {
- }
- @Override
- Public java.security.cert.x509certificate[] Getacceptedissuers () {
- return null;
- }
- };
- Sc.init (null, new trustmanager[] {TrustManager}, null);
- return SC;
- }
Then modify the original Send method:
- /**
- * Mock Request
- *
- * @param URL Resource Address
- * @param map parameter list
- * @param encoding Code
- * @return
- * @throws nosuchalgorithmexception
- * @throws keymanagementexception
- * @throws IOException
- * @throws clientprotocolexception
- */
- Public static string send (string URL, map<string,string> map,string encoding) throws Keymanagementexception, NoSuchAlgorithmException, Clientprotocolexception, IOException {
- String BODY = "";
- //handling HTTPS requests in a way that bypasses authentication
- Sslcontext Sslcontext = Createignoreverifyssl ();
- //Set protocol HTTP and HTTPS for the processing of socket-Link factory objects
- registry<connectionsocketfactory> socketfactoryregistry = registrybuilder.<connectionsocketfactory> Create ()
- . Register ("http", Plainconnectionsocketfactory.instance)
- . Register ("https", new Sslconnectionsocketfactory (sslcontext))
- . build ();
- Poolinghttpclientconnectionmanager Connmanager = new Poolinghttpclientconnectionmanager (SocketFactoryRegistry) ;
- Httpclients.custom (). Setconnectionmanager (Connmanager);
- //Create a custom HttpClient object
- Closeablehttpclient client = Httpclients.custom (). Setconnectionmanager (Connmanager). build ();
- /closeablehttpclient client = Httpclients.createdefault ();
- //Create Post method Request Object
- HttpPost HttpPost = new HttpPost (URL);
- //Reload Parameters
- list<namevaluepair> Nvps = new arraylist<namevaluepair> ();
- if (map!=null) {
- for (entry<string, string> entry:map.entrySet ()) {
- Nvps.add (New Basicnamevaluepair (Entry.getkey (), Entry.getvalue ()));
- }
- }
- //Set parameters to the request object
- Httppost.setentity (new Urlencodedformentity (Nvps, encoding));
- System.out.println ("Request address:" +url);
- System.out.println ("request parameter:" +nvps.tostring ());
- //Set Header information
- //Specify message Header "Content-type", "User-agent"
- Httppost.setheader ("Content-type", "application/x-www-form-urlencoded");
- Httppost.setheader ("user-agent", "mozilla/4.0" (compatible; MSIE 5.0; Windows NT; Digext) ");
- //Perform request operation and get results (synchronous blocking)
- Closeablehttpresponse response = Client.execute (HttpPost);
- //Get result entity
- httpentity entity = response.getentity ();
- if (Entity! = null) {
- //Convert result entity to string type by specified encoding
- BODY = entityutils.tostring (entity, encoding);
- }
- Entityutils.consume (entity);
- //Release link
- Response.close ();
- return body;
- }
Now we have to test it and find out.
The next article describes another scenario, and you should look forward to your own generated certificate.
Transfer from http://blog.csdn.net/xiaoxian8023
Easy to play with HttpClient configuration SSL, using bypass certificate authentication to implement HTTPS