Javax.net.ssl.SSLException:Unrecognized SSL message, plaintext connection

Source: Internet
Author: User
Tags log log

SSL one-way authentication and SSL bidirectional authentication when the client sends data to the server in two cases

1.SSL One-way verification

The code is as follows:

Java code
  1. Import java.io.IOException;
  2. Import Java.util.HashMap;
  3. Import Java.util.Map;
  4. Import org.apache.commons.httpclient.HttpClient;
  5. Import org.apache.commons.httpclient.HttpException;
  6. Import Org.apache.commons.httpclient.HttpStatus;
  7. Import Org.apache.commons.httpclient.NameValuePair;
  8. Import Org.apache.commons.httpclient.methods.PostMethod;
  9. Import Org.apache.commons.httpclient.params.HttpClientParams;
  10. Import Org.apache.commons.httpclient.params.HttpMethodParams;
  11. Import Org.apache.commons.logging.Log;
  12. Import Org.apache.commons.logging.LogFactory;
  13. Public class Clientsenddata {
  14. static Log log = Logfactory.getlog (clientsenddata.   Class);
  15. private String Url;
  16. //Initialize data
  17. Public Clientsenddata () {
  18. URL = "Https://test.yihaodian.com:8443/ims/feedbackToPingAn_getData.action";
  19. }
  20. Public string SendData (String data) {
  21. String receiveddata = null;
  22. try {
  23. map<string, string> paramsdata = new hashmap<string, string> ();
  24. Paramsdata.put ("data", data);
  25. Receiveddata = Send (URL, paramsdata);
  26. } catch (Exception e) {
  27. E.printstacktrace ();
  28. }
  29. return receiveddata;
  30. }
  31. public static string send (string URL, map<string, string> paramsmap) {
  32. String result = null;
  33. Postmethod Postmethod = null;
  34. HttpClient HttpClient = new HttpClient ();
  35. Httpclient.getparams (). Setparameter (
  36. Httpmethodparams.http_content_charset, "Utf-8");
  37. Postmethod = new Postmethod (URL);
  38. if (paramsmap! = null && paramsmap.size () > 0) {
  39. namevaluepair[] datas = new Namevaluepair[paramsmap.size ()];
  40. int index = 0;
  41. For (String Key:paramsMap.keySet ()) {
  42. datas[index++] = new Namevaluepair (Key, Paramsmap.get (key));
  43. }
  44. Postmethod.setrequestbody (datas);
  45. }
  46. Httpclientparams httparams = new Httpclientparams ();
  47. Httparams.setsotimeout (60000);
  48. Postmethod.setparams (Httparams);
  49. try {
  50. int statusCode = Httpclient.executemethod (Postmethod);
  51. if (StatusCode = = HTTPSTATUS.SC_OK) {
  52. result = Postmethod.getresponsebodyasstring ();
  53. Log.info ("sent successfully!  ");
  54. } Else {
  55. Log.error ("HTTP response status is" + StatusCode);
  56. }
  57. } catch (HttpException e) {
  58. Log.error ("error url=" + URL, e);
  59. } catch (IOException e) {
  60. Log.error ("error url=" + URL, e);
  61. } finally {
  62. if (postmethod! = null) {
  63. Postmethod.releaseconnection ();
  64. }
  65. }
  66. return result;
  67. }
  68. public static void Main (string[] args) {
  69. Clientsenddata t = new Clientsenddata ();
  70. T.senddata ("Test SSL single connection, send data to the server!");
  71. }
  72. }

Exceptions that may occur

1.java.net.connectexception:connection Refused:connect

Server does not start

2. Javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX Path Building failed

The certificate on the server side is not trustworthy. Workaround See this article http://zhuyuehua.iteye.com/blog/1102347

3.java.net.socketexception:software caused connection Abort:recv failed

This is because the server is configured with SSL two-way authentication, and the client sends the data according to the one-way authentication, that is, the client certificate information is not sent to the service side.

4.org.apache.commons.httpclient.nohttpresponseexception

This is generally the cause of the server-side firewall. The client request was intercepted.

Additionally, this issue occurs when the server is overloaded.

5.javax.net.ssl.sslhandshakeexception:remote host closed connection during handshake

This is because the server is configured with SSL two-way authentication, and the client sends the data according to the one-way authentication, that is, the client certificate information is not sent to the service side. When the server authenticates the client certificate, it discovers that the client does not have a certificate and then disconnects the handshake.

2.SSL bidirectional authentication

Two-way verification, temporarily do not know how to send data with httpclient, such as the need for two-way verification when sending data, refer to my other articles. In addition, you have to know how httpclient in two-way verification to send data, please advise.

Javax.net.ssl.SSLException:Unrecognized SSL message, plaintext connection

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.