JAVA发送HttpClient请求

た 入场券 2022-04-11 09:15 450阅读 0赞
  1. //测试例子:
  2. public static JSONObject test() {
  3. String urlstr = "http://xxx:8080/test/test.do";
  4. String Authorization = "test:test";
  5. JSONObject result = HttpRequestUtils.httpPost(urlstr, null, Authorization);
  6. return result;
  7. }
  8. public class HttpRequestUtils {
  9. protected final Logger logger = LoggerFactory.getLogger(HttpRequestUtils.class); // 日志记录
  10. // 表示请求器是否已经做了初始化工作
  11. private static boolean hasInit = false;
  12. // 连接超时时间,默认10秒
  13. private static int socketTimeout = 10000;
  14. // 传输超时时间,默认30秒
  15. private static int connectTimeout = 30000;
  16. // 请求器的配置
  17. private static RequestConfig requestConfig;
  18. public static void init() {
  19. if (!hasInit) {
  20. requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
  21. }
  22. }
  23. public static JSONObject httpPost(String url, JSONObject param, String Authorization) {
  24. init();
  25. // post请求返回结果
  26. CloseableHttpClient httpClient = HttpClients.createDefault();
  27. JSONObject result = null;
  28. HttpPost httpPost = new HttpPost();
  29. if (param != null) {
  30. // 解决中文乱码问题
  31. StringEntity entity = new StringEntity(param.toString(), "UTF-8");
  32. entity.setContentEncoding("UTF-8");
  33. entity.setContentType("application/json");
  34. httpPost.setEntity(entity);
  35. }
  36. // 增加http basic authenticate认证
  37. if (!Tools.isEmpty(Authorization)) {
  38. String encoding;
  39. try {
  40. encoding = Base64.encode(Authorization.getBytes("UTF-8"));
  41. httpPost.setHeader("Authorization", String.format("Basic %s", encoding));
  42. } catch (UnsupportedEncodingException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. // 设置请求器的配置
  47. httpPost.setConfig(requestConfig);
  48. try {
  49. HttpResponse response = httpClient.execute(httpPost);
  50. // 请求发送成功,得到响应
  51. int responseCode = response.getStatusLine().getStatusCode();
  52. if (response.getStatusLine().getStatusCode() == 200) {
  53. String str = EntityUtils.toString(response.getEntity(),"UTF-8");
  54. try {
  55. result = new JSONObject(str);
  56. } catch (org.json.JSONException e) {
  57. result = new JSONObject();
  58. result.put("str", str);
  59. }
  60. } else {
  61. result = new JSONObject();
  62. result.put("responseCode", responseCode);
  63. }
  64. } catch (ClientProtocolException e) {
  65. e.printStackTrace();
  66. } catch (IOException e) {
  67. e.printStackTrace();
  68. } finally {
  69. try {
  70. httpPost.releaseConnection();
  71. httpPost.abort();
  72. httpClient.close();
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. return result;
  78. }
  79. public static JSONObject httpGet(String url, String Authorization) {
  80. init();
  81. // get请求返回结果
  82. JSONObject result = null;
  83. CloseableHttpClient httpClient = HttpClients.createDefault();
  84. // 发送get请求
  85. HttpGet httpGet = new HttpGet(url);
  86. // 增加http basic authenticate认证
  87. if (!Tools.isEmpty(Authorization)) {
  88. String encoding;
  89. try {
  90. encoding = Base64.encode(Authorization.getBytes("UTF-8"));
  91. httpGet.setHeader("Authorization", String.format("Basic %s", encoding));
  92. } catch (UnsupportedEncodingException e) {
  93. e.printStackTrace();
  94. }
  95. }
  96. // 设置请求器的配置
  97. httpGet.setConfig(requestConfig);
  98. try {
  99. HttpResponse response = httpClient.execute(httpGet);
  100. // 请求发送成功,得到响应
  101. int responseCode = response.getStatusLine().getStatusCode();
  102. if (responseCode == 200) {
  103. String str = EntityUtils.toString(response.getEntity(),"UTF-8");
  104. try {
  105. result = new JSONObject(str);
  106. } catch (org.json.JSONException e) {
  107. result = new JSONObject();
  108. result.put("responseCode", responseCode);
  109. }
  110. }
  111. } catch (ClientProtocolException e) {
  112. e.printStackTrace();
  113. } catch (IOException e) {
  114. e.printStackTrace();
  115. } finally {
  116. try {
  117. httpGet.releaseConnection();
  118. httpGet.abort();
  119. httpClient.close();
  120. } catch (IOException e) {
  121. e.printStackTrace();
  122. }
  123. }
  124. return result;
  125. }
  126. }

发表评论

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

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

相关阅读