javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection
客户端向服务器发送数据时,份两种情况,SSL单向验证和SSL双向验证
1.SSL单向验证时
代码如下:
- 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;
- // 初始化数据
- 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("发送成功!");
- } 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("测试SSL单项连接,向服务端发送数据!");
- }
- }
可能出现的异常
1.java.net.ConnectException: Connection refused: connect
服务器没有启动
2 .javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed
服务端的证书是不可信的。解决办法见这篇文章 http://zhuyuehua.iteye.com/blog/1102347
3.java.net.SocketException: Software caused connection abort: recv failed
这是由于服务端配置的是SSL双向认证,而客户端发送数据是按照服务器是单向认证时发送的,即没有将客户端证书信息一起发送给服务端。
4.org.apache.commons.httpclient.NoHttpResponseException
这一般是服务端防火墙的原因。拦截了客户端请求。
另外,当服务端负载过重时,也会出现此问题。
5.javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
这是由于服务端配置的是SSL双向认证,而客户端发送数据是按照服务器是单向认证时发送的,即没有将客户端证书信息一起发送给服务端。服务端验证客户端证书时,发现客户端没有证书,然后就断开了握手连接。
2.SSL双向验证时
双向验证时,暂时不知道如何用HTTPCLIENT发送数据,如需要双向验证时发送数据,参考我另外的文章。另外,有知道HTTPCLIENT如何在双向验证时发送数据的,恳请指教。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。