带参数的GET请求 - JEECG 模块
package org.jeecg.modules.biz.jop; import java.io.IOException; import java.net.URISyntaxException; import java.text.SimpleDateFormat; import java.util.; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import lombok.extern.log4j.Log4j2; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.jeecg.common.util.StringUtils; import org.jeecg.modules.biz.dto.JhAndMqjbResponse; import org.jeecg.modules.biz.entity.; import org.jeecg.modules.biz.service.*; import org.json.JSONException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; /**
-
@author gmy
-
@version 1.0
-
@Date 2021/7/29 10:25
-
带参数的get请求 */ @Component @Configurable @Log4j2 public class HttpGetParamTest {
@Autowired private ITaskDistributeService taskDistributeService;
@Autowired private MaterialsFeedService materialsFeedService; @Autowired private MaterialsPlanService materialsPlanService;
@Autowired private MaterialsPersonalService materialsPersonalService;
@Autowired private MaterialsCarService materialsCarService;
@Autowired private MaterialsPlanInfoService materialsPlanInfoService;
@Autowired private MaterialsDetailsService materialsDetailsService;
@Autowired private MaterialsQtperService materialsQtperService;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private String tokenUrl = "http://10.66.1.80:5002/SSOService/checkAppKey?appKey=GDZYYXGLXT&appSecret=Gdzyyx@2023&login=gdxt"; private String jxzjsgzjUrl = "http://10.66.1.80:5002/DasService/DataService/slepbp/slepbw/jxzy/jxzygl/gzlyssq"; private String planUrl ="http://10.66.1.80:5002/DasService/DataService/slepbp/slepbw/jxzy/jxzygl/gzlys_sggx"; private String glxxUrl ="http://10.66.1.80:5002/DasService/DataService/slepbp/slepbw/jxzy/jxzygl/gzlyssq_glxx"; private String lwxxUrl ="http://10.66.1.80:5002/DasService/DataService/slepbp/slepbw/jxzy/jxzygl/gzlys_phlw"; private String tcxxUrl ="http://10.66.1.80:5002/DasService/DataService/slepbp/slepbw/jxzy/jxzygl/gzlyssq_tzclxx"; private String qtxxUrl ="http://10.66.1.80:5002/DasService/DataService/slepbp/slepbw/jxzy/jxzygl/gzlys_qtlw";
// public static void main(String[] args) throws JSONException, URISyntaxException, java.text.ParseException, IOException, InterruptedException { // String tokenUrl = "http://10.66.1.80:5002/SSOService/checkAppKey?appKey=GDZYYXGLXT&appSecret=Gdzyyx@2023&login=gdxt"; // String jxzjsgzjUrl = "http://10.66.1.80:5002/DasService/DataService/slepbp/slepbw/jxzy/jxzygl/gzlyssq"; // String planUrl ="http://10.66.1.80:5002/DasService/DataService/slepbp/slepbw/jxzy/jxzygl/gzlys_sggx"; // String glxxUrl ="http://10.66.1.80:5002/DasService/DataService/slepbp/slepbw/jxzy/jxzygl/gzlyssq_glxx"; // String lwxxUrl ="http://10.66.1.80:5002/DasService/DataService/slepbp/slepbw/jxzy/jxzygl/gzlys_phlw"; // String tcxxUrl ="http://10.66.1.80:5002/DasService/DataService/slepbp/slepbw/jxzy/jxzygl/gzlyssq_tzclxx"; // String qtxxUrl ="http://10.66.1.80:5002/DasService/DataService/slepbp/slepbw/jxzy/jxzygl/gzlys_qtlw"; // HttpGetParamTest httpGetParamTest = new HttpGetParamTest(); // String token = httpGetParamTest.getToken(tokenUrl); // httpGetParamTest.getGxUrl(jxzjsgzjUrl,token,"fe754099a0c54f5899f474d2cd450e46"); // } @Async public String getToken(String tokenUrl) throws JSONException { CloseableHttpClient httpClient = HttpClients.createDefault(); String entityStr = null; CloseableHttpResponse response = null; // 获取连接客户端工具 try { URIBuilder uriBuilder = new URIBuilder(tokenUrl); HttpGet httpGet = new HttpGet(uriBuilder.build()); // 传输的类型 httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded"); // 执行请求 response = httpClient.execute(httpGet); // 获得响应的实体对象 HttpEntity entity = response.getEntity(); // 使用Apache提供的工具类进行转换成字符串 entityStr = EntityUtils.toString(entity, "UTF-8"); } catch (ClientProtocolException e) { System.err.println("Http协议出现问题"); e.printStackTrace(); } catch (ParseException e) { System.err.println("解析错误"); e.printStackTrace(); } catch (URISyntaxException e) { System.err.println("URI解析异常"); e.printStackTrace(); } catch (IOException e) { System.err.println("IO异常"); e.printStackTrace(); } finally { // 释放连接 if (null != response) { try { response.close(); httpClient.close(); } catch (IOException e) { System.err.println("释放连接出错"); e.printStackTrace(); } } } JSONObject parse = (JSONObject) JSONObject.parse(entityStr.toString()); //获取toker log.error("token---------" + parse.getString("token")); return parse.getString("token"); } @Async public void getjxZjSgZj(String jxzjsgzjUrl, String token) throws URISyntaxException, IOException, java.text.ParseException, JSONException { log.error("进入getjxZjSgZj---"); // 获取连接客户端工具 CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; HttpEntity entity = null; JSONObject parse = null; JSONArray jsonObjList = null; String entityStr= null; JSONObject data=null; /** 添加请求头信息*/ boolean a = true; int i = 0; List
ids = new ArrayList<>(); bb: while (a) { URIBuilder uriBuilder = new URIBuilder(jxzjsgzjUrl); List list = new LinkedList<>(); BasicNameValuePair param1 = new BasicNameValuePair("login", "gdxt"); //井号 BasicNameValuePair param2 = new BasicNameValuePair("cjdwdm_str", "3020110000"); //获取当月第一天 BasicNameValuePair param3 = new BasicNameValuePair("sgqsrq_str", "2023-01-01"); list.add(param1); list.add(param2); list.add(param3); uriBuilder.setParameters(list); // 根据带参数的URI对象构建GET请求对象 HttpGet httpGet = new HttpGet(uriBuilder.build()); log.error("i---进入" + i); // 浏览器表示 httpGet.addHeader("token", token); httpGet.addHeader("pageSize", "20"); httpGet.addHeader("startIndex", i + ""); // 传输的类型 httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded"); // 执行请求 response = httpClient.execute(httpGet); HttpClients.custom().setConnectionManagerShared(true); // 获得响应的实体对象 entity = response.getEntity(); entityStr = EntityUtils.toString(entity, "UTF-8"); parse = (JSONObject) JSONObject.parse(entityStr); data = (JSONObject) parse.getJSONArray("data").get(0); jsonObjList = data.getJSONArray("datas"); if (!jsonObjList.isEmpty()) { for (Object o : jsonObjList) { JSONObject jsonObject = (JSONObject) o; MaterialsDetails materials = materialsDetailsService.getById(jsonObject.getString("dbid")); //判断 作业是否完成 if (materials == null){ log.error("查询到没有的---" + jsonObject.getString("dbid")); if (!StringUtils.isEmpty(jsonObject.getString("sgzzrq"))) { saveMaterialsDetails(jsonObject); SavePlanId("http://10.66.1.80:5002/DasService/DataService/slepbp/slepbw/jxzy/jxzygl/gzlys_sggx",jsonObject.getString("dbid"),token); ids.add(jsonObject.getString("dbid")); }else { //判断工序中有没有交井这个工序 List<Map<String, String>> dbid = getGxUrl("http://10.66.1.80:5002/DasService/DataService/slepbp/slepbw/jxzy/jxzygl/gzlys_sggx", token, jsonObject.getString("dbid")); if (dbid!=null){ aa: for (Map<String, String> stringStringMap : dbid) { log.error("这个工序----" + stringStringMap.get("jxzyxcsgxmmc")); if (!StringUtils.isEmpty(stringStringMap.get("jxzyxcsgxmmc")) && stringStringMap.get("jxzyxcsgxmmc").equals("交井") ){ log.equals("交井------" + i); saveMaterialsDetails(jsonObject); ids.add(jsonObject.getString("dbid")); break aa; } } } } } } }else { break bb; } i++; entityStr=null; parse=null; } log.error("抽取结束" + new Date()); for (int j = 0; i < ids.size(); j++) { log.error("iiiiiiiiiiiiiiiii------------" + j); getGlxxUrl("http://10.66.1.80:5002/DasService/DataService/slepbp/slepbw/jxzy/jxzygl/gzlyssq_glxx",ids.get(j),token); getLwxxUrl("http://10.66.1.80:5002/DasService/DataService/slepbp/slepbw/jxzy/jxzygl/gzlys_phlw",ids.get(j),token); getTcxxUrl("http://10.66.1.80:5002/DasService/DataService/slepbp/slepbw/jxzy/jxzygl/gzlyssq_tzclxx",ids.get(j),token); getQtxxUrl("http://10.66.1.80:5002/DasService/DataService/slepbp/slepbw/jxzy/jxzygl/gzlys_qtlw",ids.get(j),token); getGxUrl("http://10.66.1.80:5002/DasService/DataService/slepbp/slepbw/jxzy/jxzygl/gzlys_sggx",ids.get(j),token); } }
内容:@Async
public void getGlxxUrl(String glxxUrl, String id, String token) throws URISyntaxException, IOException, java.text.ParseException, JSONException {
log.error("进入getGlxxUrl---");
// 获取连接客户端工具
CloseableHttpClient httpClient = HttpClients.createDefault();
URIBuilder uriBuilder = new URIBuilder(glxxUrl);
List
@Async
public void getTcxxUrl(String tcxxUrl, String id, String token) throws URISyntaxException, IOException, java.text.ParseException, JSONException {
log.error("进入getTcxxUrl---");
// 获取连接客户端工具
CloseableHttpClient httpClient = HttpClients.createDefault();
URIBuilder uriBuilder = new URIBuilder(tcxxUrl);
List<NameValuePair> list = new LinkedList<>();
BasicNameValuePair param1 = new BasicNameValuePair("login", "gdxt"); //井号
BasicNameValuePair param2 = new BasicNameValuePair("cjdwdm_str", "3020110000");
//获取当月第一天
BasicNameValuePair param3 = new BasicNameValuePair("sgqsrq_str", "2023-01-01");
list.add(param1);
list.add(param2);
list.add(param3);
uriBuilder.setParameters(list);
// 根据带参数的URI对象构建GET请求对象
HttpGet httpGet = new HttpGet(uriBuilder.build());
// 浏览器表示
httpGet.addHeader("token", token);
httpGet.addHeader("entityId", id);
// 传输的类型
httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");
CloseableHttpResponse response=null;
HttpEntity entity=null;
String entityStr=null;
JSONObject parse=null;
JSONArray data=null;
// 执行请求
response = httpClient.execute(httpGet);
// 获得响应的实体对象
entity = response.getEntity();
entityStr = EntityUtils.toString(entity, "UTF-8");
parse = (JSONObject) JSONObject.parse(entityStr);
data = (JSONArray) parse.getJSONArray("data").get(0);
JSONObject jsonObject= (JSONObject) data.get(0);
log.error(jsonObject.toString());
saveMaterialsPersonal(jsonObject);
}
@Async
public void getQtxxUrl(String qtxxUrl, String id, String token) throws URISyntaxException, IOException, java.text.ParseException, JSONException {
log.error("进入getQtxxUrl---");
// 获取连接客户端工具
CloseableHttpClient httpClient = HttpClients.createDefault();
URIBuilder uriBuilder = new URIBuilder(qtxxUrl);
List<NameValuePair> list = new LinkedList<>();
BasicNameValuePair param1 = new BasicNameValuePair("login", "gdxt"); //井号
BasicNameValuePair param2 = new BasicNameValuePair("cjdwdm_str", "3020110000");
//获取当月第一
原文地址: https://www.cveoy.top/t/topic/pCZe 著作权归作者所有。请勿转载和采集!