Chinaunix首页 | 论坛 | 博客
  • 博客访问: 318905
  • 博文数量: 287
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2965
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(287)

文章存档

2022年(85)

2021年(201)

2019年(1)

我的朋友

分类: Java

2022-01-20 20:15:45


点击(此处)折叠或打开


  1. ackage org.fh.util;

  2. import java.io.BufferedReader;
  3. import java.io.DataOutputStream;
  4. import java.io.InputStreamReader;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. import java.util.List;
  8. import java.util.Map;

  9. /**
  10.  * 说明:http 工具类
  11.  * 作者:FH Admin
  12.  * from:fhadmin.cn
  13.  */
  14. public class HttpUtil {

  15.     public static String post(String requestUrl, String accessToken, String params)
  16.             throws Exception {
  17.         String contentType = "application/x-www-form-urlencoded";
  18.         return HttpUtil.post(requestUrl, accessToken, contentType, params);
  19.     }

  20.     public static String post(String requestUrl, String accessToken, String contentType, String params)
  21.             throws Exception {
  22.         String encoding = "UTF-8";
  23.         if (requestUrl.contains("nlp")) {
  24.             encoding = "GBK";
  25.         }
  26.         return HttpUtil.post(requestUrl, accessToken, contentType, params, encoding);
  27.     }

  28.     public static String post(String requestUrl, String accessToken, String contentType, String params, String encoding)
  29.             throws Exception {
  30.         String url = requestUrl + "?access_token=" + accessToken;
  31.         return HttpUtil.postGeneralUrl(url, contentType, params, encoding);
  32.     }

  33.     public static String postGeneralUrl(String generalUrl, String contentType, String params, String encoding)
  34.             throws Exception {
  35.         URL url = new URL(generalUrl);
  36.         // 打开和URL之间的连接
  37.         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  38.         connection.setRequestMethod("POST");
  39.         // 设置通用的请求属性
  40.         connection.setRequestProperty("Content-Type", contentType);
  41.         connection.setRequestProperty("Connection", "Keep-Alive");
  42.         connection.setUseCaches(false);
  43.         connection.setDoOutput(true);
  44.         connection.setDoInput(true);

  45.         // 得到请求的输出流对象
  46.         DataOutputStream out = new DataOutputStream(connection.getOutputStream());
  47.         out.write(params.getBytes(encoding));
  48.         out.flush();
  49.         out.close();

  50.         // 建立实际的连接
  51.         connection.connect();
  52.         // 获取所有响应头字段
  53.         Map<String, List<String>> headers = connection.getHeaderFields();
  54.         // 遍历所有的响应头字段
  55.         for (String key : headers.keySet()) {
  56.             System.err.println(key + "--->" + headers.get(key));
  57.         }
  58.         // 定义 BufferedReader输入流来读取URL的响应
  59.         BufferedReader in = null;
  60.         in = new BufferedReader(
  61.                 new InputStreamReader(connection.getInputStream(), encoding));
  62.         String result = "";
  63.         String getLine;
  64.         while ((getLine = in.readLine()) != null) {
  65.             result += getLine;
  66.         }
  67.         in.close();
  68.         System.err.println("result:" + result);
  69.         return result;
  70.     }
  71. }


阅读(183) | 评论(0) | 转发(0) |
0

上一篇:springboot 跨域配置类

下一篇:java 中定时器

给主人留下些什么吧!~~