java使用httpclient封装post请求

古城微笑少年丶 2023-10-17 21:48 109阅读 0赞

我们程序员在项目开发过程中,经常用到接口,目前比较流行httpclient 技术。以下是我之前封装的httpclient的post请求 ,希望对大家有所帮助:

精简代码如下:可以直接复制使用

  1. public static String postTDH(String url, String body, String mimeType,
  2. String charset, Integer connTimeout, Integer readTimeout) throws IOException, GeneralSecurityException
  3. {
  4. String result = "";
  5. HttpClient client = null;
  6. HttpPost post = new HttpPost(url);
  7. try {
  8. /*HttpEntity entity = new StringEntity(body, ContentType.create(
  9. mimeType, charset));
  10. post.setEntity(entity);*/
  11. StringEntity s = new StringEntity(body.toString(),
  12. Charset.forName("utf-8"));
  13. s.setContentEncoding("UTF-8");
  14. post.setEntity(s);
  15. // 设置参数
  16. Builder customReqConf = RequestConfig.custom();
  17. if (connTimeout != null) {
  18. customReqConf.setConnectTimeout(connTimeout);
  19. }
  20. if (readTimeout != null) {
  21. customReqConf.setSocketTimeout(readTimeout);
  22. }
  23. post.setConfig(customReqConf.build());
  24. HttpResponse res;
  25. if (url.startsWith("https")) {
  26. // 执行 Https 请求.
  27. client = createSSLInsecureClient();
  28. res = client.execute(post);
  29. } else {
  30. // 执行 Http 请求.
  31. client = HttpClientUtils.client;
  32. res = client.execute(post);
  33. }
  34. int statusCode = res.getStatusLine().getStatusCode();
  35. System.out.println("返回码:"+statusCode);
  36. //result = IOUtils.toString(res.getEntity().getContent(), charset);
  37. result = UnicodeToZ.decodeUnicode(EntityUtils
  38. .toString(res.getEntity()));
  39. logger.info("################发送报文内容:################");
  40. logger.info(body);
  41. logger.info("################返回报文内容:################");
  42. logger.info(result);
  43. }finally {
  44. post.releaseConnection();
  45. if (url.startsWith("https") && client != null
  46. && client instanceof CloseableHttpClient) {
  47. ((CloseableHttpClient) client).close();
  48. }
  49. }
  50. return result;
  51. }
  52. Integer connTimeout, Integer readTimeout;接口超时时间设置
  53. UnicodeToZ.decodeUnicode(EntityUtils .toString(res.getEntity()));返回的报文进行中文装换(选择性使用)
  54. public class UnicodeToZ {
  55. public static String decodeUnicode(String theString) {
  56. char aChar;
  57. int len = theString.length();
  58. StringBuffer outBuffer = new StringBuffer(len);
  59. for (int x = 0; x < len;) {
  60. aChar = theString.charAt(x++);
  61. if (aChar == '\\') {
  62. aChar = theString.charAt(x++);
  63. if (aChar == 'u') {
  64. // Read the xxxx
  65. int value = 0;
  66. for (int i = 0; i < 4; i++) {
  67. aChar = theString.charAt(x++);
  68. switch (aChar) {
  69. case '0':
  70. case '1':
  71. case '2':
  72. case '3':
  73. case '4':
  74. case '5':
  75. case '6':
  76. case '7':
  77. case '8':
  78. case '9':
  79. value = (value << 4) + aChar - '0';
  80. break;
  81. case 'a':
  82. case 'b':
  83. case 'c':
  84. case 'd':
  85. case 'e':
  86. case 'f':
  87. value = (value << 4) + 10 + aChar - 'a';
  88. break;
  89. case 'A':
  90. case 'B':
  91. case 'C':
  92. case 'D':
  93. case 'E':
  94. case 'F':
  95. value = (value << 4) + 10 + aChar - 'A';
  96. break;
  97. default:
  98. throw new IllegalArgumentException(
  99. "Malformed \\uxxxx encoding.");
  100. }
  101. }
  102. outBuffer.append((char) value);
  103. } else {
  104. if (aChar == 't')
  105. aChar = '\t';
  106. else if (aChar == 'r')
  107. aChar = '\r';
  108. else if (aChar == 'n')
  109. aChar = '\n';
  110. else if (aChar == 'f')
  111. aChar = '\f';
  112. outBuffer.append(aChar);
  113. }
  114. } else
  115. outBuffer.append(aChar);
  116. }
  117. return outBuffer.toString();
  118. }
  119. }

改转换方法 大家可以详细研究。有不明白的地方大家可以留言,博主会及时帮你解决。

发表评论

表情:
评论列表 (有 0 条评论,109人围观)

还没有评论,来说两句吧...

相关阅读