敏捷工具
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.2 KiB

2 years ago
  1. package com.whn.hellospring.service;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.stereotype.Service;
  4. import java.io.*;
  5. import java.net.URL;
  6. import java.net.URLConnection;
  7. import java.util.List;
  8. import java.util.Map;
  9. @Slf4j
  10. @Service
  11. public class JuheService {
  12. public String getOilPrice() {
  13. String url = "http://apis.juhe.cn/gnyj/query?key=212b6526c4f8a3899ca406a1173bafa8";
  14. String result = "";
  15. BufferedReader in = null;
  16. try {
  17. String urlNameString = url;
  18. URL realUrl = new URL(urlNameString);
  19. URLConnection connection = realUrl.openConnection();
  20. connection.setRequestProperty("accept", "*/*");
  21. connection.setRequestProperty("connection", "Keep-Alive");
  22. connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  23. connection.connect();
  24. Map<String, List<String>> map = connection.getHeaderFields();
  25. for (String key : map.keySet()) {
  26. System.out.println(key + "--->" + map.get(key));
  27. }
  28. in = new BufferedReader(new InputStreamReader(
  29. connection.getInputStream()));
  30. String line;
  31. while ((line = in.readLine()) != null) {
  32. result += line;
  33. }
  34. } catch (Exception e) {
  35. System.out.println("异常" + e);
  36. e.printStackTrace();
  37. } finally {
  38. try {
  39. if (in != null) {
  40. in.close();
  41. }
  42. } catch (Exception e2) {
  43. e2.printStackTrace();
  44. }
  45. }
  46. return result;
  47. }
  48. public static String sendPost(String url, String param) {
  49. PrintWriter out = null;
  50. BufferedReader in = null;
  51. String result = "";
  52. try {
  53. URL realUrl = new URL(url);
  54. URLConnection conn = realUrl.openConnection();
  55. conn.setRequestProperty("Content-Type", "application/json");
  56. conn.setRequestProperty("charset", "utf-8");
  57. conn.setRequestProperty("accept", "*/*");
  58. conn.setRequestProperty("connection", "Keep-Alive");
  59. conn.setRequestProperty("user-agent",
  60. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  61. conn.setDoOutput(true);
  62. conn.setDoInput(true);
  63. out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "utf-8"));
  64. out.print(param);
  65. out.flush();
  66. in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
  67. String line;
  68. while ((line = in.readLine()) != null) {
  69. result += line;
  70. }
  71. System.out.println("sendPost" + result);
  72. } catch (Exception e) {
  73. System.out.println("异常" + e);
  74. e.printStackTrace();
  75. } finally {
  76. try {
  77. if (out != null) {
  78. out.close();
  79. }
  80. if (in != null) {
  81. in.close();
  82. }
  83. } catch (IOException ex) {
  84. ex.printStackTrace();
  85. }
  86. }
  87. return result;
  88. }
  89. }