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
- Import java.io.IOException;
- Import Java.util.HashMap;
- Import Java.util.Map;
- Import org.apache.commons.httpclient.HttpClient;
- Import org.apache.commons.httpclient.HttpException;
- Import Org.apache.commons.httpclient.HttpStatus;
- Import Org.apache.commons.httpclient.NameValuePair;
- Import Org.apache.commons.httpclient.methods.PostMethod;
- Import Org.apache.commons.httpclient.params.HttpClientParams;
- Import Org.apache.commons.httpclient.params.HttpMethodParams;
- Import Org.apache.commons.logging.Log;
- Import Org.apache.commons.logging.LogFactory;
- Public class Clientsenddata {
- static Log log = Logfactory.getlog (clientsenddata. Class);
- private String Url;
- //Initialize data
- Public Clientsenddata () {
- URL = "Https://test.yihaodian.com:8443/ims/feedbackToPingAn_getData.action";
- }
- Public string SendData (String data) {
- String receiveddata = null;
- try {
- map<string, string> paramsdata = new hashmap<string, string> ();
- Paramsdata.put ("data", data);
- Receiveddata = Send (URL, paramsdata);
- } catch (Exception e) {
- E.printstacktrace ();
- }
- return receiveddata;
- }
- public static string send (string URL, map<string, string> paramsmap) {
- String result = null;
- Postmethod Postmethod = null;
- HttpClient HttpClient = new HttpClient ();
- Httpclient.getparams (). Setparameter (
- Httpmethodparams.http_content_charset, "Utf-8");
- Postmethod = new Postmethod (URL);
- if (paramsmap! = null && paramsmap.size () > 0) {
- namevaluepair[] datas = new Namevaluepair[paramsmap.size ()];
- int index = 0;
- For (String Key:paramsMap.keySet ()) {
- datas[index++] = new Namevaluepair (Key, Paramsmap.get (key));
- }
- Postmethod.setrequestbody (datas);
- }
- Httpclientparams httparams = new Httpclientparams ();
- Httparams.setsotimeout (60000);
- Postmethod.setparams (Httparams);
- try {
- int statusCode = Httpclient.executemethod (Postmethod);
- if (StatusCode = = HTTPSTATUS.SC_OK) {
- result = Postmethod.getresponsebodyasstring ();
- Log.info ("sent successfully! ");
- } Else {
- Log.error ("HTTP response status is" + StatusCode);
- }
- } catch (HttpException e) {
- Log.error ("error url=" + URL, e);
- } catch (IOException e) {
- Log.error ("error url=" + URL, e);
- } finally {
- if (postmethod! = null) {
- Postmethod.releaseconnection ();
- }
- }
- return result;
- }
- public static void Main (string[] args) {
- Clientsenddata t = new Clientsenddata ();
- T.senddata ("Test SSL single connection, send data to the server!");
- }
- }
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