LAPTOP-KB9HII50\70708 2 лет назад
Родитель
Сommit
453d7d1743

+ 27 - 4
common/common-util/src/main/java/com/yihu/jw/util/http/HttpClientUtil.java

@ -7,16 +7,15 @@ import com.yihu.jw.entity.base.dict.BaseExceptionServerDictDO;
import com.yihu.jw.util.dao.BaseExceptionDictUtilDao;
import com.yihu.jw.util.dao.BaseExceptionLogUtilDao;
import com.yihu.jw.util.dao.BaseExceptionServerDictUtilDao;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
@ -393,6 +392,30 @@ public class HttpClientUtil {
        restTemplate.put(url, formEntity, String.class);
    }
    public JSONObject headerPost(String url,MultiValueMap<String, String> params){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/x-www-form-urlencoded");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        org.springframework.http.HttpEntity<MultiValueMap<String, String>> request = new org.springframework.http.HttpEntity<>(params, headers);
        org.springframework.http.HttpEntity<JSONObject> response = restTemplate.exchange(url, HttpMethod.POST, request, JSONObject.class);
        return response.getBody();
    }
    public String getBodyAuthorization(String url, String token, String str) throws IOException {
        RestTemplate restTemplate = new RestTemplate();
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url+str);
        httpGet.addHeader("Authorization","Bearer"+" "+token);
        httpGet.addHeader("Type",MediaType.APPLICATION_JSON_VALUE);
        httpGet.addHeader("Accept",MediaType.ALL_VALUE);
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String strss = EntityUtils.toString(entity, "UTF-8") ;
        return strss;
    }
    /**
     * 发送post请求
     *

+ 69 - 15
svr/svr-cloud-care/src/main/java/com/yihu/jw/care/util/HongLingJinUtil.java

@ -9,9 +9,16 @@ import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -32,38 +39,85 @@ public class HongLingJinUtil {
    @Autowired
    private HttpClientUtil httpClientUtil;
    public void getToken(){
    /**
     * 获取token
     * @return
     */
    public String getToken(){
        try {
            String url = "http://10.18.40.99:7680/sso/token";
//            String url = "https://zhyzh.gongshu.gov.cn/ssotoken/sso/token";
//            String url = "https://zhyzh.gongshu.gov.cn/ssotoken";
            JSONObject json = new JSONObject();
            json.put("client_id","client_third");
            json.put("client_secret","a2o8W3nP");
            json.put("username",username);
            Map<String,Object> httpPost = new HashedMap();
            httpPost.put("Content-Type", "application/x-www-form-urlencoded");
            String code = RSAUtils.encrypt(json.toString(),publicKey);
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("code", code));
            String response = httpClientUtil.headerPost(url, params,"UTF-8",httpPost);
            logger.info("url:"+url);
            logger.info("code:"+code);
            JSONObject response = httpClientUtil.headerPost(url+"?code="+code, null);
            if(response.getInteger("code")==200){
                return  response.getJSONObject("data").getString("access_token");
            }
            logger.info("response:"+response);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public static void main(String[] args) throws Exception{
//        String url = "http://10.18.40.101:7688/api/zzd/common";
        String url = "https://zhyzh.gongshu.gov.cn/zzdcommon";
        JSONObject params = new JSONObject();
        JSONObject eventAssistName = new JSONObject();
        JSONObject test1 = new JSONObject();
        test1.put("deptName","");
        test1.put("mobile","");
        test1.put("realName","");
        eventAssistName.put("test",test1);
        JSONObject eventChargeName = new JSONObject();
        JSONObject test2 = new JSONObject();
        test2.put("deptName","");
        test2.put("mobile","15659713528");
        test2.put("realName","测试账号");
/*        test1.put("mobile","15545430148");
        test1.put("realName","李康硕");*/
        eventChargeName.put("test",test2);
        params.put("eventAssistName",eventAssistName);
        params.put("eventChargeName",eventChargeName);
        params.put("eventHappenTime","2022-07-13 21:27:00");
        params.put("eventLocation","测试地址");
        params.put("eventName","测试事件");
        params.put("taskDescription","测试事件1");
        params.put("eventState","0");
        params.put("taskId",0);
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Bearer "+"a8d1f682-9281-4d88-84ed-3643e738da09");
        headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);
        headers.add("Accept",MediaType.ALL_VALUE);
        org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
        String ret = restTemplate.postForObject(url, formEntity, String.class);
        System.out.println(ret);
    }
    /**
     *
     */
    public void zzdcommon(){
//        String url = "http://10.18.40.99:7680/sso/token";
        String url = "https://zhyzh.gongshu.gov.cn/zzdcommon/api/zzd/common";
        JSONObject json = new JSONObject();
        json.put("client_id","client_third");
        json.put("client_secret","a2o8W3nP");
        json.put("username",username);
        try {
            String url = "http://10.18.40.101:7688/api/zzd/common";
//            String url = "https://zhyzh.gongshu.gov.cn/zzdcommon";
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}