HttpUtils.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package com.goafanti.common.utils;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import org.apache.http.HttpResponse;
  5. import org.apache.http.HttpStatus;
  6. import org.apache.http.ParseException;
  7. import org.apache.http.client.HttpClient;
  8. import org.apache.http.client.methods.HttpGet;
  9. import org.apache.http.client.methods.HttpPost;
  10. import org.apache.http.entity.StringEntity;
  11. import org.apache.http.impl.client.HttpClientBuilder;
  12. import org.apache.http.util.EntityUtils;
  13. import java.io.IOException;
  14. import java.net.UnknownHostException;
  15. import java.nio.charset.Charset;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. import java.util.Set;
  19. public class HttpUtils {
  20. public static JSONObject httpGet(String url) {
  21. JSONObject jsonResult = null;
  22. try {
  23. HttpClient client = HttpClientBuilder.create().build();//获取DefaultHttpClient请求
  24. HttpGet request = new HttpGet(url);
  25. HttpResponse response = client.execute(request);
  26. if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  27. String strResult = EntityUtils.toString(response.getEntity(),"UTF-8");//此处设定编码格式
  28. jsonResult = JSON.parseObject(strResult);
  29. } else {
  30. LoggerUtils.error(HttpUtils.class,"HttpStatus异常");
  31. }
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35. return jsonResult;
  36. }
  37. public static JSONObject httpPost(String url,Map<String, Object>map){
  38. return httpPost(url,map,0);
  39. }
  40. public static JSONObject httpPostBody(String url,Map<String, Object>map){
  41. return httpPost(url,map,1);
  42. }
  43. /**
  44. *
  45. * @param url 地址
  46. * @param map 参数
  47. * @param type 0 json 1body
  48. * @return
  49. */
  50. public static JSONObject httpPost(String url,Map<String, Object>map,Integer type) {
  51. HttpClient httpClient = HttpClientBuilder.create().build();
  52. HttpPost httpPost = new HttpPost(url);
  53. httpPost.setHeader("Accept", "application/json");
  54. if (type==0){
  55. httpPost.addHeader("Content-type", "application/json");
  56. httpPost.setEntity(new StringEntity(JSON.toJSONString(map), Charset.forName("UTF-8")));
  57. }else{
  58. httpPost.addHeader("Content-type", "application/x-www-form-urlencoded");
  59. httpPost.setEntity(new StringEntity(ParamMap(map), Charset.forName("UTF-8")));
  60. }
  61. HttpResponse response = null;
  62. try {
  63. response = httpClient.execute(httpPost);
  64. if (null == response || response.getStatusLine() == null) {
  65. throw new Exception("Post Request For Url[{}] is not ok. Response is null");
  66. } else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
  67. throw new Exception("Post Request For Url[{}] is not ok. Response Status Code is {"+response.getStatusLine().getStatusCode()+"}");
  68. }
  69. } catch (UnknownHostException uhe) {
  70. LoggerUtils.error(HttpUtils.class,uhe.getMessage());
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. }
  74. String resultString=null;
  75. try {
  76. if (response != null) {
  77. resultString = EntityUtils.toString(response.getEntity());
  78. }
  79. } catch (ParseException | IOException e) {
  80. e.printStackTrace();
  81. }
  82. return JSONObject.parseObject(resultString);
  83. }
  84. private static String ParamMap(Map<String, Object> map) {
  85. StringBuffer str =new StringBuffer();
  86. Set<String> strings = map.keySet();
  87. for (String string : strings) {
  88. str=str.append(string).append("=").append(map.get(string)).append("&");
  89. }
  90. String substring = str.substring(0, str.length() - 1);
  91. return substring;
  92. }
  93. public static void main(String[] args) {
  94. // JSONObject jsonObject = httpGet("https://oapi.dingtalk.com/gettoken?appkey=dingdstdmazod2ghnj2y&appsecret=E4b_1VJaL1_m3hUr2D4vvoP3KzLQpHB5pibRmWRr8cwq28vLPbpgFUO4MuryH39n");
  95. // System.out.println(jsonObject.toJSONString());
  96. //{"errcode":0,"access_token":"3c9596da886b366084a1e02a6683e2f9","errmsg":"ok","expires_in":7200}
  97. Map<String ,Object >param=new HashMap<>();
  98. param.put("userid","144431256935772788");
  99. param.put("work_date","2024-03-01");
  100. JSONObject jsonObject = httpPost("https://oapi.dingtalk.com/topapi/attendance/getupdatedata?access_token=6b2ab08b2c5a327cb86276fb34dbaf10", param, 1);
  101. System.out.println(jsonObject.toJSONString());
  102. }
  103. }