Easy to play with HttpClient configuration SSL, using bypass certificate authentication to implement HTTPS

Source: Internet
Author: User

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:

  1. Public static void Main (string[] args) throws ParseException, IOException, Keymanagementexception, NoSuchAlgorithmException, httpprocessexception {
  2. String url = "Https://sso.tgb.com:8443/cas/login";
  3. String BODY = Send (URL, null, "Utf-8");
  4. SYSTEM.OUT.PRINTLN ("Transaction response result:");
  5. System.out.println (body);
  6. System.out.println ("-----------------------------------");
  7. }

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:

  1. /**
  2. * Bypass Authentication
  3. *
  4. * @return
  5. * @throws nosuchalgorithmexception
  6. * @throws keymanagementexception
  7. */
  8. Public static Sslcontext Createignoreverifyssl () throws NoSuchAlgorithmException, keymanagementexception {
  9. Sslcontext sc = sslcontext.getinstance ("SSLv3");
  10. //Implement a X509trustmanager interface for bypassing validation without modifying the method inside
  11. X509trustmanager TrustManager = new X509trustmanager () {
  12. @Override
  13. public void checkclienttrusted (
  14. Java.security.cert.x509certificate[] Paramarrayofx509certificate,
  15. String paramstring) throws certificateexception {
  16. }
  17. @Override
  18. public void checkservertrusted (
  19. Java.security.cert.x509certificate[] Paramarrayofx509certificate,
  20. String paramstring) throws certificateexception {
  21. }
  22. @Override
  23. Public java.security.cert.x509certificate[] Getacceptedissuers () {
  24. return null;
  25. }
  26. };
  27. Sc.init (null, new trustmanager[] {TrustManager}, null);
  28. return SC;
  29. }

Then modify the original Send method:

  1. /**
  2. * Mock Request
  3. *
  4. * @param URL Resource Address
  5. * @param map parameter list
  6. * @param encoding Code
  7. * @return
  8. * @throws nosuchalgorithmexception
  9. * @throws keymanagementexception
  10. * @throws IOException
  11. * @throws clientprotocolexception
  12. */
  13. Public static string send (string URL, map<string,string> map,string encoding) throws Keymanagementexception, NoSuchAlgorithmException, Clientprotocolexception, IOException {
  14. String BODY = "";
  15. //handling HTTPS requests in a way that bypasses authentication
  16. Sslcontext Sslcontext = Createignoreverifyssl ();
  17. //Set protocol HTTP and HTTPS for the processing of socket-Link factory objects
  18. registry<connectionsocketfactory> socketfactoryregistry = registrybuilder.<connectionsocketfactory> Create ()
  19. . Register ("http", Plainconnectionsocketfactory.instance)
  20. . Register ("https", new Sslconnectionsocketfactory (sslcontext))
  21. . build ();
  22. Poolinghttpclientconnectionmanager Connmanager = new Poolinghttpclientconnectionmanager (SocketFactoryRegistry)  ;
  23. Httpclients.custom (). Setconnectionmanager (Connmanager);
  24. //Create a custom HttpClient object
  25. Closeablehttpclient client = Httpclients.custom (). Setconnectionmanager (Connmanager). build ();
  26. /closeablehttpclient client = Httpclients.createdefault ();
  27. //Create Post method Request Object
  28. HttpPost HttpPost = new HttpPost (URL);
  29. //Reload Parameters
  30. list<namevaluepair> Nvps = new arraylist<namevaluepair> ();
  31. if (map!=null) {
  32. for (entry<string, string> entry:map.entrySet ()) {
  33. Nvps.add (New Basicnamevaluepair (Entry.getkey (), Entry.getvalue ()));
  34. }
  35. }
  36. //Set parameters to the request object
  37. Httppost.setentity (new Urlencodedformentity (Nvps, encoding));
  38. System.out.println ("Request address:" +url);
  39. System.out.println ("request parameter:" +nvps.tostring ());
  40. //Set Header information
  41. //Specify message Header "Content-type", "User-agent"
  42. Httppost.setheader ("Content-type", "application/x-www-form-urlencoded");
  43. Httppost.setheader ("user-agent", "mozilla/4.0" (compatible; MSIE 5.0; Windows NT;  Digext) ");
  44. //Perform request operation and get results (synchronous blocking)
  45. Closeablehttpresponse response = Client.execute (HttpPost);
  46. //Get result entity
  47. httpentity entity = response.getentity ();
  48. if (Entity! = null) {
  49. //Convert result entity to string type by specified encoding
  50. BODY = entityutils.tostring (entity, encoding);
  51. }
  52. Entityutils.consume (entity);
  53. //Release link
  54. Response.close ();
  55. return body;
  56. }

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

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.