代码调用外部HTTP请求接口

浅浅的花香味﹌ 2022-03-09 17:13 449阅读 0赞

转载链接样例:https://www.cnblogs.com/guxiong/p/6661272.html
https://zhidao.baidu.com/question/627561819246516844.html

  1. package net.ninehkj.logistics.util;
  2. import java.io.IOException;
  3. import java.io.UnsupportedEncodingException;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import java.util.Map;
  7. import javax.annotation.Resource;
  8. import org.apache.http.NameValuePair;
  9. import org.apache.http.client.ResponseHandler;
  10. import org.apache.http.client.entity.UrlEncodedFormEntity;
  11. import org.apache.http.client.methods.CloseableHttpResponse;
  12. import org.apache.http.client.methods.HttpPost;
  13. import org.apache.http.entity.StringEntity;
  14. import org.apache.http.impl.client.BasicResponseHandler;
  15. import org.apache.http.impl.client.CloseableHttpClient;
  16. import org.apache.http.impl.client.HttpClients;
  17. import org.apache.http.message.BasicNameValuePair;
  18. import org.apache.http.util.EntityUtils;
  19. /**
  20. * 调用HTTP接口返回数据
  21. * @author Administrator
  22. *
  23. */
  24. public class InterfaceUtils {
  25. @Resource
  26. private CloseableHttpClient httpClient;
  27. /**
  28. * Post请求(此方法会返回字符串)
  29. * @param payUrl 路径
  30. * @param param 请求数据
  31. * @return
  32. * @throws UnsupportedEncodingException
  33. * 将map集合转换为json字符串
  34. * JSONObject jsonObject = JSONObject.fromObject(map);
  35. */
  36. public static String JSONPostHttpCallOtherInterfaceUtils(String payUrl, String json) throws UnsupportedEncodingException {
  37. /**
  38. * 创建Httpclient对象
  39. */
  40. CloseableHttpClient httpClient = HttpClients.createDefault();
  41. ResponseHandler<String> responseHandler = new BasicResponseHandler();
  42. String result = "";
  43. try {
  44. /**
  45. * 创建HTTP请求并放入请求地址
  46. */
  47. HttpPost httpPost = new HttpPost(payUrl);
  48. if(!"".equals(json)) {
  49. /**
  50. * 将数据放入UrlEncodedFormEntity会自动将数据编码为合适内容
  51. */
  52. StringEntity requestEntity = new StringEntity(json, "utf-8");
  53. requestEntity.setContentEncoding("UTF-8");
  54. /**
  55. * 给httpPost设置JSON格式的参数
  56. */
  57. httpPost.setHeader("Content-type", "application/json");
  58. httpPost.setEntity(requestEntity);
  59. /**
  60. * 发送HTTP请求获取返回值
  61. */
  62. result = httpClient.execute(httpPost, responseHandler);
  63. }
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. } finally {
  67. try {
  68. httpClient.close();
  69. } catch (IOException e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. return result;
  74. }
  75. /**
  76. * Post请求
  77. * @param payUrl 路径
  78. * @param param 请求数据
  79. * @return
  80. * @throws UnsupportedEncodingException
  81. */
  82. public static String PostHttpCallOtherInterfaceUtils(String payUrl, Map<String, String> param) throws UnsupportedEncodingException {
  83. /**
  84. * 创建Httpclient对象
  85. */
  86. CloseableHttpClient httpClient = HttpClients.createDefault();
  87. CloseableHttpResponse response = null;
  88. String result = "";
  89. try {
  90. /**
  91. * 创建HTTP请求并放入请求地址
  92. */
  93. HttpPost httpPost = new HttpPost(payUrl);
  94. if(null != param) {
  95. /**
  96. * 此集合用于存储传送的数据
  97. */
  98. List<NameValuePair> list = new LinkedList<NameValuePair>();
  99. for (String key : param.keySet()) {
  100. /**
  101. * 遍历map中的参数放入集合中
  102. * BasicNameValuePair对象中传入参数该map的key和当前key对应的value值
  103. * 为什么使用BasicNameValuePair,因为我们在下面使用UrlEncodedFormEntity
  104. * 对BasicNameValuePair数据进行编码时只能接受List<? extends NameValuePair>
  105. * 为参数。链接:https://blog.csdn.net/zdb292034/article/details/80663792
  106. *
  107. */
  108. list.add(new BasicNameValuePair(key, param.get(key)));
  109. }
  110. /**
  111. * 将数据放入UrlEncodedFormEntity会自动将数据编码为合适内容:param1=value1¶m2=value2
  112. * 如果内容不规范,可以使用StringEntity
  113. */
  114. UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list);
  115. httpPost.setEntity(urlEncodedFormEntity);
  116. /**
  117. * 进行HTTP请求
  118. */
  119. response = httpClient.execute(httpPost);
  120. // httpClient
  121. /**
  122. * 执行http请求
  123. */
  124. result = EntityUtils.toString(response.getEntity(), "utf-8");
  125. }
  126. } catch (Exception e) {
  127. e.printStackTrace();
  128. } finally {
  129. if(null != response) {
  130. try {
  131. response.close();
  132. } catch (IOException e) {
  133. e.printStackTrace();
  134. }
  135. }
  136. }
  137. return result;
  138. }
  139. }

发表评论

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

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

相关阅读