| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- package com.goafanti.common.utils;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import org.apache.http.HttpResponse;
- import org.apache.http.HttpStatus;
- import org.apache.http.ParseException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.HttpClientBuilder;
- import org.apache.http.util.EntityUtils;
- import java.io.IOException;
- import java.net.UnknownHostException;
- import java.nio.charset.Charset;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Set;
- public class HttpUtils {
- public static JSONObject httpGet(String url) {
- JSONObject jsonResult = null;
- try {
- HttpClient client = HttpClientBuilder.create().build();//获取DefaultHttpClient请求
- HttpGet request = new HttpGet(url);
- HttpResponse response = client.execute(request);
- if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
- String strResult = EntityUtils.toString(response.getEntity(),"UTF-8");//此处设定编码格式
- jsonResult = JSON.parseObject(strResult);
- } else {
- LoggerUtils.error(HttpUtils.class,"HttpStatus异常");
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return jsonResult;
- }
- public static JSONObject httpPost(String url,Map<String, Object>map){
- return httpPost(url,map,0);
- }
- public static JSONObject httpPostBody(String url,Map<String, Object>map){
- return httpPost(url,map,1);
- }
- /**
- *
- * @param url 地址
- * @param map 参数
- * @param type 0 json 1body
- * @return
- */
- public static JSONObject httpPost(String url,Map<String, Object>map,Integer type) {
- HttpClient httpClient = HttpClientBuilder.create().build();
- HttpPost httpPost = new HttpPost(url);
- httpPost.setHeader("Accept", "application/json");
- if (type==0){
- httpPost.addHeader("Content-type", "application/json");
- httpPost.setEntity(new StringEntity(JSON.toJSONString(map), Charset.forName("UTF-8")));
- }else{
- httpPost.addHeader("Content-type", "application/x-www-form-urlencoded");
- httpPost.setEntity(new StringEntity(ParamMap(map), Charset.forName("UTF-8")));
- }
- HttpResponse response = null;
- try {
- response = httpClient.execute(httpPost);
- if (null == response || response.getStatusLine() == null) {
- throw new Exception("Post Request For Url[{}] is not ok. Response is null");
- } else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
- throw new Exception("Post Request For Url[{}] is not ok. Response Status Code is {"+response.getStatusLine().getStatusCode()+"}");
- }
- } catch (UnknownHostException uhe) {
- LoggerUtils.error(HttpUtils.class,uhe.getMessage());
- } catch (Exception e) {
- e.printStackTrace();
- }
- String resultString=null;
- try {
- if (response != null) {
- resultString = EntityUtils.toString(response.getEntity());
- }
- } catch (ParseException | IOException e) {
- e.printStackTrace();
- }
- return JSONObject.parseObject(resultString);
- }
- private static String ParamMap(Map<String, Object> map) {
- StringBuffer str =new StringBuffer();
- Set<String> strings = map.keySet();
- for (String string : strings) {
- str=str.append(string).append("=").append(map.get(string)).append("&");
- }
- String substring = str.substring(0, str.length() - 1);
- return substring;
- }
- public static void main(String[] args) {
- // JSONObject jsonObject = httpGet("https://oapi.dingtalk.com/gettoken?appkey=dingdstdmazod2ghnj2y&appsecret=E4b_1VJaL1_m3hUr2D4vvoP3KzLQpHB5pibRmWRr8cwq28vLPbpgFUO4MuryH39n");
- // System.out.println(jsonObject.toJSONString());
- //{"errcode":0,"access_token":"3c9596da886b366084a1e02a6683e2f9","errmsg":"ok","expires_in":7200}
- Map<String ,Object >param=new HashMap<>();
- param.put("userid","144431256935772788");
- param.put("work_date","2024-03-01");
- JSONObject jsonObject = httpPost("https://oapi.dingtalk.com/topapi/attendance/getupdatedata?access_token=6b2ab08b2c5a327cb86276fb34dbaf10", param, 1);
- System.out.println(jsonObject.toJSONString());
- }
- }
|