需求:由于最近公司需要调用别人写好得接口,需要各种数据转换,手动调用一个小时才几十条,有7000多条数据,所以自己写了一个自动化调用得代码进行调用
首先我们需要获取网站登录得cookie才能调用
public static String getCookie() {
String cookie = "";
URL = "/chis/logon/myApps?urt=23397&uid=311999&pwd=123&deep=3";
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
StringBuffer params = new StringBuffer();
JSONObject json = JSONObject.parseObject("{'url': 'logon/myApps?urt=23397&uid=311999&pwd=123&deep=3', 'httpMethod': 'POST'}");
/*catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}*/
HttpPost httpGet = new HttpPost(DOMAIN + URL);
System.out.println("请求路径" + httpGet);
StringEntity entity = new StringEntity(json.toString(), "UTF-8");
httpGet.setHeader("Content-Type", "application/json");
httpGet.setEntity(entity);
CloseableHttpResponse response = null;
try {
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setSocketTimeout(5000)
.setRedirectsEnabled(true).build();
httpGet.setConfig(requestConfig);
try {
response = httpClient.execute(httpGet);
String setCookie = response.toString();
int startCookieIndex = setCookie.indexOf(("Set-Cookie: ")) + 12;
int endCookieIndex = setCookie.indexOf(("; Path"));
cookie = setCookie.substring(startCookieIndex, endCookieIndex);
} catch (IOException e) {
e.printStackTrace();
}
HttpEntity responseEntity = response.getEntity();
System.out.println("get请求响应状态为:" + response.getStatusLine());
if (responseEntity != null) {
byte[] bytes = EntityUtils.toByteArray(responseEntity);
System.out.println("====未做处理===" + bytes.toString());
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
InputStreamReader inputStreamReader = new InputStreamReader(byteArrayInputStream);
BufferedReader br = new BufferedReader(inputStreamReader);
String s = null;
StringBuilder builder = new StringBuilder();
while ((s = br.readLine()) != null) {
builder.append(s);
}
String result = builder.toString();
JSONObject jsonObject = JSONObject.parseObject(result);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return cookie;
}
然后我们需要将json数据转为对象
public static <T> List<T> getOrderObjectsByClass(String jsonName , String jsonPath, Class<T> type){
LinkedList<T> returnList = new LinkedList<T>();
File file = new File(jsonPath);
InputStreamReader isr = null;
BufferedReader bufferedReader = null;
try {
isr = new InputStreamReader(new FileInputStream(file), "utf-8");
bufferedReader = new BufferedReader(isr);
JSONReader reader = new JSONReader(bufferedReader);
reader.startArray();
int a=0;
while (reader.hasNext()) {a++;
T readObject = reader.readObject(type);
returnList.add(readObject);
}
System.out.println("进来"+a);
reader.endArray();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
try {
if(null != isr){
isr.close();
}
if(null != bufferedReader){
bufferedReader.close();
}
} catch (Exception e2) {
}
}
return returnList;
}
转换完之后调用接口 跟 别人得网站数据转为一致
public static boolean callBasicHealthLoginGetCookieAndSavaOrUpdateDrugs(Drugs drugs) {
int a=0;
String savaOrUpdate = "";
if (drugs != null) {
Class clazz = drugs.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
if (field.get(drugs) == null) {
System.out.println("field.get(drugs1)" + field.get(drugs));
field.set(drugs, "");
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
savaOrUpdate = "update";
} else {
savaOrUpdate = "create";
}
Map map = new HashMap();
Map map2=new HashMap();
map.put("serviceId", "chis.drugManageService");
map.put("schema", "chis.application.pub.schemas.SQ_MBYP");
map.put("op", savaOrUpdate);
map.put("method", "execute");
map.put("serviceAction", "saveCDMaintainDrug");
map2.put("YPXH",Integer.parseInt(drugs.getYpxh()));
map2.put("DL",drugs.getDl());
map2.put("LBXH",drugs.getLbxh());
map2.put("YPMC",drugs.getYpmc());
map2.put("PYM",drugs.getPym());
map2.put("QTMC1",drugs.getQtmc1());
map2.put("PYM1",drugs.getPym1());
map2.put("PYM2",drugs.getPym2());
map2.put("QTMC2",drugs.getQtmc2());
map2.put("QTMC3",drugs.getQtmc3());
map2.put("PYM3",drugs.getPym3());
map2.put("QTMC4",drugs.getQtmc4());
map2.put("PYM4",drugs.getPym4());
map2.put("QTMC5",drugs.getQtmc5());
map2.put("PYM5",drugs.getPym5());
map2.put("LBMC",drugs.getLbmc());
map2.put("ZJFLAG",drugs.getZjflag());
map2.put("SJXH",drugs.getSjxh());
map2.put("YPGG",drugs.getYpgg());
map2.put("YPDW",drugs.getYpdw());
map2.put("YPJL",drugs.getYpjl());
map2.put("JLDW",drugs.getJldw());
map2.put("YCJL",drugs.getYcjl());
map.put("body",map2);
JSONObject json = new JSONObject(map);
System.out.println("json================================》" + json);
savaOrUpdateDrugs(getCookie(), json);
return true;
}
最后调用接口修改
public static boolean savaOrUpdateDrugs(String cookie, JSONObject jsonParam) {
a++;
URL = "/chis/*.jsonRequest";
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
StringBuffer params = new StringBuffer();
/*catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}*/
HttpPost httpGet = new HttpPost(DOMAIN + URL);
System.out.println("请求路径" + httpGet);
StringEntity entity = new StringEntity(jsonParam.toString(), "UTF-8");
httpGet.setHeader("Content-Type", "application/json");
httpGet.setHeader("Cookie", cookie);
httpGet.setEntity(entity);
CloseableHttpResponse response = null;
try {
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setSocketTimeout(5000)
.setRedirectsEnabled(true).build();
httpGet.setConfig(requestConfig);
try {
response = httpClient.execute(httpGet);
System.out.println(response);
} catch (IOException e) {
e.printStackTrace();
}
HttpEntity responseEntity = response.getEntity();
System.out.println("get请求响应状态为:" + response.getStatusLine());
if (responseEntity != null) {
System.out.println("get请求响应内容长度为:" + responseEntity.getContentLength());
byte[] bytes = EntityUtils.toByteArray(responseEntity);
System.out.println("====未做处理===" + bytes.toString());
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
InputStreamReader inputStreamReader = new InputStreamReader(byteArrayInputStream);
BufferedReader br = new BufferedReader(inputStreamReader);
String s = null;
StringBuilder builder = new StringBuilder();
while ((s = br.readLine()) != null) {
builder.append(s);
}
String result = builder.toString();
System.out.println("get请求响应内容为:" + result);
System.out.println("================================================>第"+a);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
|