HTTP request no peer certificate Solution

Source: Internet
Author: User

Many users encounter javax.net. SSL. sslpeerunverifiedexception: no peer certificate exceptions when making HTTP requests for Sina authorization or Sina data. The solution is provided (tested ).


Write a custom class to inherit sslsocketfactory:

  1. Import java. Io. ioexception;
  2. Import java.net. Socket;
  3. Import java.net. unknownhostexception;
  4. Import java. Security. keymanagementexception;
  5. Import java. Security. keystore;
  6. Import java. Security. keystoreexception;
  7. Import java. Security. nosuchalgorithmexception;
  8. Import java. Security. unrecoverablekeyexception;

  9. Import javax.net. SSL. sslcontext;
  10. Import javax.net. SSL. trustmanager;
  11. Import javax.net. SSL. x509trustmanager;

  12. Import org. Apache. http. Conn. SSL. sslsocketfactory;

  13. Public class sslsocketfactoryex extends sslsocketfactory {

  14. Sslcontext = sslcontext. getinstance ("TLS ");

  15. Public sslsocketfactoryex (keystore truststore)
  16. Throws nosuchalgorithmexception, keymanagementexception,
  17. Keystoreexception, unrecoverablekeyexception {
  18. Super (truststore );

  19. Trustmanager TM = new x509trustmanager (){
  20. Public java. Security. cert. x509certificate [] getacceptedissuers () {return NULL ;}

  21. @ Override
  22. Public void checkclienttrusted (
  23. Java. Security. cert. x509certificate [] Chain, string authtype)
  24. Throws java. Security. cert. certificateexception {}

  25. @ Override
  26. Public void checkservertrusted (
  27. Java. Security. cert. x509certificate [] Chain, string authtype)
  28. Throws java. Security. cert. certificateexception {}
  29. };
  30. Sslcontext. INIT (null, new trustmanager [] {TM}, null );
  31. }

  32. @ Override
  33. Public socket createsocket (Socket socket, string host, int port, Boolean autoclose) throws ioexception, unknownhostexception {
  34. Return sslcontext. getsocketfactory (). createsocket (socket, host, port, autoclose );
  35. }

  36. @ Override
  37. Public socket createsocket () throws ioexception {
  38. Return sslcontext. getsocketfactory (). createsocket ();
  39. }
  40. }
Copy code



Let's take a look at how to perform callback:

  1. Public static httpclient getnewhttpclient (){
  2. Try {
  3. Keystore truststore = keystore. getinstance (keystore. getdefaulttype ());
  4. Truststore. Load (null, null );

  5. Sslsocketfactory Sf = new sslsocketfactoryex (truststore );
  6. SF. sethostnameverifier (sslsocketfactory. allow_all_hostname_verifier );

  7. Httpparams Params = new basichttpparams ();
  8. Httpprotocolparams. setversion (Params, httpversion. http_1_1 );
  9. Httpprotocolparams. setcontentcharset (Params, HTTP. utf_8 );

  10. Schemeregistry registry = new schemeregistry ();
  11. Registry. Register (New Scheme ("HTTP", plainsocketfactory. getsocketfactory (), 80 ));
  12. Registry. Register (New Scheme ("HTTPS", SF, 443 ));

  13. Clientconnectionmanager CCM = new threadsafeclientconnmanager (Params, registry );

  14. Return new defaulthttpclient (CCM, Params );
  15. } Catch (exception e ){
  16. Return new defaulthttpclient ();
  17. }
  18. }
Copy code

Now, you can use this httpclient to request data !!!!! Good

 

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.