curl 可以终端请求数据
image.png

java实现

  1. package com.sjk.utils;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. /**
  6. * Create by wyb 2022/1/11 4:36 PM
  7. * description :
  8. */
  9. public class CurlUtil {
  10. public static String execCurl(String[] cmds) {
  11. ProcessBuilder process = new ProcessBuilder(cmds);
  12. Process p;
  13. try {
  14. p = process.start();
  15. BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
  16. StringBuilder builder = new StringBuilder();
  17. String line = null;
  18. while ((line = reader.readLine()) != null) {
  19. builder.append(line);
  20. builder.append(System.getProperty("line.separator"));
  21. }
  22. return builder.toString();
  23. } catch (IOException e) {
  24. System.out.print("error");
  25. e.printStackTrace();
  26. }
  27. return null;
  28. }
  29. public static void main(String[] args) {
  30. String[] cmds = {
  31. "curl", "-X", "GET", "http://aabb.com/aa/bb/cc",
  32. "-H", "param1: value1",
  33. "-H", "param2: value2"
  34. };
  35. /**
  36. // post
  37. curl -X POST 'http://aabb.com/aa/bb/cc' -H 'Content-Type: application/json' -H 'param1: value1'
  38. -d "{'bodyparam1':'bodyparam2'}"
  39. */
  40. String resultStr = execCurl(cmds);
  41. }
  42. }