Springboot封装HTTPClient中get,post,json,put请求方法

谁践踏了优雅 2023-09-25 14:01 75阅读 0赞

pom.xml依赖如下

  1. <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
  2. <dependency>
  3. <groupId>org.apache.httpcomponents</groupId>
  4. <artifactId>httpclient</artifactId>
  5. <version>4.5.6</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
  8. <dependency>
  9. <groupId>com.alibaba</groupId>
  10. <artifactId>fastjson</artifactId>
  11. <version>1.2.75</version>
  12. </dependency>
  13. <!-- https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path -->
  14. <dependency>
  15. <groupId>com.jayway.jsonpath</groupId>
  16. <artifactId>json-path</artifactId>
  17. <version>2.4.0</version>
  18. </dependency>

封装get,post请求,贴代码

  1. public class HttpRequestUtil {
  2. public static String getRequest(String url, Map<String,String> params){
  3. String res = "";
  4. boolean flag = true;
  5. Set<String> keys = params.keySet();
  6. for (String key : keys) {
  7. if (flag){
  8. url += "?" + key + "=" + params.get(key);
  9. flag = false;
  10. }else {
  11. url += "&" + key + "=" + params.get(key);
  12. }
  13. }
  14. HttpGet httpGet = new HttpGet(url);
  15. HttpClient httpClient = HttpClients.createDefault();
  16. try {
  17. HttpResponse response = httpClient.execute(httpGet);
  18. res = EntityUtils.toString(response.getEntity());
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. } finally {
  22. try {
  23. ((CloseableHttpClient) httpClient).close();
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. return res;
  29. }
  30. public static String postRequest(String url, Map<String,String> params){
  31. String res = "";
  32. HttpPost httpPost = new HttpPost(url);
  33. ArrayList<BasicNameValuePair> basicNameValuePairs = new ArrayList<BasicNameValuePair>();
  34. // 遍历map,放到basicNameValuePairs中
  35. Set<String> keys = params.keySet();
  36. for (String key : keys) {
  37. basicNameValuePairs.add(new BasicNameValuePair(key,params.get(key)));
  38. }
  39. HttpClient httpClient = HttpClients.createDefault();
  40. try {
  41. httpPost.setEntity(new UrlEncodedFormEntity(basicNameValuePairs,"utf-8"));
  42. HttpResponse httpResponse = httpClient.execute(httpPost);
  43. res = EntityUtils.toString(httpResponse.getEntity());
  44. } catch (Exception e) {
  45. e.printStackTrace();
  46. } finally {
  47. try {
  48. ((CloseableHttpClient) httpClient).close();
  49. } catch (IOException e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. return res;
  54. }
  55. public static String sendRequest(String url, String requestType, Map<String, String> parameters){
  56. String response = "";
  57. if ("get".equalsIgnoreCase(requestType)){
  58. response = getRequest(url, parameters);
  59. }else if ("post".equalsIgnoreCase(requestType)){
  60. response = postRequest(url, parameters);
  61. }else {
  62. response = "error request type!!!";
  63. }
  64. return response;
  65. }
  66. }

封装json,put请求,贴代码

  1. public class HttpRequestJsonUtil {
  2. // 声明为静态方法
  3. public static String postRequest(String url, JSONObject jsonObject, JSONObject headers){
  4. String res = "";
  5. HttpPost httpPost = new HttpPost(url);
  6. // 通过形参设置请求头
  7. Set<String> headerkeys = headers.keySet();
  8. for (String headerkey : headerkeys) {
  9. httpPost.addHeader(headerkey.trim(),headers.getString(headerkey).trim());
  10. }
  11. // 发送 json 类型数据
  12. httpPost.setEntity(new StringEntity(jsonObject.toString(),"UTF-8"));
  13. CloseableHttpClient httpClient = HttpClients.createDefault();
  14. // 发送请求
  15. HttpResponse httpResponse = null;
  16. try {
  17. httpResponse = httpClient.execute(httpPost);
  18. res = EntityUtils.toString(httpResponse.getEntity());
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. } finally {
  22. try {
  23. httpClient.close();
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. return res;
  29. }
  30. public static String sendRequest(String url, String requestType, JSONObject jsonObject, JSONObject headers){
  31. String response = "";
  32. if ("post".equalsIgnoreCase(requestType)){
  33. response = postRequest(url, jsonObject, headers);
  34. }else {
  35. response = "error request type!!!";
  36. }
  37. return response;
  38. }
  39. }

下一章会出很详细的!

发表评论

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

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

相关阅读