当前位置: 代码迷 >> Web前端 >> 客户端怎么使用httpclient向https服务器发送数据
  详细解决方案

客户端怎么使用httpclient向https服务器发送数据

热度:1041   发布时间:2012-08-25 10:06:20.0
客户端如何使用httpclient向https服务器发送数据

客户端向服务器发送数据时,份两种情况,SSL单向验证和SSL双向验证

?

1.SSL单向验证时

?

代码如下:

Java代码 ?收藏代码
  1. import ?java.io.IOException;??
  2. import ?java.util.HashMap;??
  3. import ?java.util.Map;??
  4. ??
  5. import ?org.apache.commons.httpclient.HttpClient;??
  6. import ?org.apache.commons.httpclient.HttpException;??
  7. import ?org.apache.commons.httpclient.HttpStatus;??
  8. import ?org.apache.commons.httpclient.NameValuePair;??
  9. import ?org.apache.commons.httpclient.methods.PostMethod;??
  10. import ?org.apache.commons.httpclient.params.HttpClientParams;??
  11. import ?org.apache.commons.httpclient.params.HttpMethodParams;??
  12. import ?org.apache.commons.logging.Log;??
  13. import ?org.apache.commons.logging.LogFactory;??
  14. ??
  15. public ? class ?ClientSendData?{??
  16. ????static ?Log?log?=?LogFactory.getLog(ClientSendData. class );??
  17. ??
  18. ????private ?String?Url;??
  19. ??
  20. ????//?初始化数据 ??
  21. ????public ?ClientSendData()?{??
  22. ????????Url?=?"https://test.yihaodian.com:8443/ims/feedbackToPingAn_getData.action" ;??
  23. ????}??
  24. ??
  25. ????public ?String?sendData(String?data)?{??
  26. ????????String?receivedData?=?null ;??
  27. ????????try ?{??
  28. ??
  29. ????????????Map<String,?String>?paramsData?=?new ?HashMap<String,?String>();??
  30. ????????????paramsData.put("data" ,?data);??
  31. ????????????receivedData?=?send(Url,?paramsData);??
  32. ????????}?catch ?(Exception?e)?{??
  33. ????????????e.printStackTrace();??
  34. ????????}??
  35. ????????return ?receivedData;??
  36. ????}??
  37. ??
  38. ????public ? static ?String?send(String?url,?Map<String,?String>?paramsMap)?{??
  39. ????????String?result?=?null ;??
  40. ????????PostMethod?postMethod?=?null ;??
  41. ????????HttpClient?httpClient?=?new ?HttpClient();??
  42. ??
  43. ????????httpClient.getParams().setParameter(??
  44. ????????????????HttpMethodParams.HTTP_CONTENT_CHARSET,?"utf-8" );??
  45. ????????postMethod?=?new ?PostMethod(url);??
  46. ??
  47. ????????if ?(paramsMap?!=? null ?&&?paramsMap.size()?>? 0 )?{??
  48. ????????????NameValuePair[]?datas?=?new ?NameValuePair[paramsMap.size()];??
  49. ????????????int ?index?=? 0 ;??
  50. ????????????for ?(String?key?:?paramsMap.keySet())?{??
  51. ????????????????datas[index++]?=?new ?NameValuePair(key,?paramsMap.get(key));??
  52. ????????????}??
  53. ????????????postMethod.setRequestBody(datas);??
  54. ??
  55. ????????}??
  56. ??
  57. ????????HttpClientParams?httparams?=?new ?HttpClientParams();??
  58. ????????httparams.setSoTimeout(60000 );??
  59. ????????postMethod.setParams(httparams);??
  60. ??
  61. ????????try ?{??
  62. ????????????int ?statusCode?=?httpClient.executeMethod(postMethod);??
  63. ????????????if ?(statusCode?==?HttpStatus.SC_OK)?{??
  64. ????????????????result?=?postMethod.getResponseBodyAsString();??
  65. ????????????????log.info("发送成功!" );??
  66. ????????????}?else ?{??
  67. ????????????????log.error("?http?response?status?is?" ?+?statusCode);??
  68. ????????????}??
  69. ??
  70. ????????}?catch ?(HttpException?e)?{??
  71. ????????????log.error("error?url=" ?+?url,?e);??
  72. ????????}?catch ?(IOException?e)?{??
  73. ????????????log.error("error?url=" ?+?url,?e);??
  74. ????????}?finally ?{??
  75. ????????????if ?(postMethod?!=? null )?{??
  76. ????????????????postMethod.releaseConnection();??
  77. ????????????}??
  78. ????????}??
  79. ??
  80. ????????return ?result;??
  81. ????}??
  82. ??
  83. ????public ? static ? void ?main(String[]?args)?{??
  84. ????????ClientSendData?t?=?new ?ClientSendData();??
  85. ????????t.sendData("测试SSL单项连接,向服务端发送数据!" );??
  86. ????}??
  87. }??
?

可能出现的异常

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双向认证,而客户端发送数据是按照服务器是单向认证时发送的,即没有将客户端证书信息一起发送给服务端。服务端验证客户端证书时,发现客户端没有证书,然后就断开了握手连接。

?

?

  相关解决方案