前阵子因为对接公司的oa系统,遇到一些问题,因为公司oa系统代码比较老旧,没有用maven作为代码管理,用到的都包都是需要导入lib,而且导入的依赖包也不能版本太高不然是用不了的。本来对接的系统人员已经把代码打成包了,我这边测试了一下是可以直接拿来用,但是我们这边的oa运维人员说用不了,原因是公司的oa系统代码比较旧。。。
现在是写一下原生代码实现oa系统与其他系统对接接口代码 。
首先,我这边是获取到对接系统的一个接口地址url,我需要把一些人员信息给传到对方,对方把结果返回给我。
总得来说,我就写了一个类,类中集成了相关的功能。
以下是用到的lib包,都是版本比较低的。
public class OA {
//获取到结果,传一个url请求地址,appkey,密钥,方法名,人员信息对象
public static String getResult(String url, String appkey, String secret, String method, Shopemp shopemp) throws Exception {
String param = JSON.toJSONString(shopemp);//把人员信息对象转成json字符串
Map<String, String> stringStringMap = makeHttpParam(appkey, secret, method, param);//封装数据
String result = http_post(url, stringStringMap);//开始执行请求
return result;
}
//对数据进行处理封装
private static Map<String,String> makeHttpParam(String appkey, String secret, String method, String data) throws Exception {
String time = String.valueOf(Calendar.getInstance().getTimeInMillis() / 1000L);
String sign = makeSign(appkey, secret, method, time, data);
HashMap<String, String> map = new HashMap<>();
map.put("appkey", appkey);
map.put("method", method);
map.put("timestamp", time);
map.put("sign", sign);
map.put("data", data);
return map;
}
//获取到sign
private static String makeSign(String appkey, String secret, String method, String time, String data) throws Exception {
StringBuffer buffer = new StringBuffer();
buffer.append(appkey);
buffer.append(time);
buffer.append(method);
buffer.append(data);
buffer.append(secret);
String signstr = buffer.toString();
String sign = MD5Util.getMD5(signstr);
//String sign = DigestUtils.md5Hex(signText.getBytes("utf-8"));
return sign;
}
//进行数据请求发送
private static String http_post(String httpurl,Map<String,String> params) throws Exception {
HttpClient httpClient = new HttpClient();
PostMethod method = new PostMethod(httpurl);
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
httpClient.getHttpConnectionManager().getParams().setSoTimeout(120000);
method.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
if (params != null) {
Set<String> keySet = params.keySet();
for (String key : keySet) {
String value = params.get(key);
method.addParameter(key,value);//对数据进行添加到method
}
}
String response = "";
try {
int status = httpClient.executeMethod(method);//执行方法,获取返回结果状态
if (status >= 300 || status < 200) {
throw new Exception(" status:" + status + " response:" + method.getResponseBodyAsString());
}
response = parserResponse(method);//对结果进行解析处理,获取想要的数据
} finally {
method.releaseConnection();
}
return response;
}
//解析结果数据
public static String parserResponse(PostMethod method) throws IOException {
StringBuffer contentBuffer = new StringBuffer();
InputStream in = method.getResponseBodyAsStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, method.getResponseCharSet()));
String inputLine = null;
while((inputLine = reader.readLine()) != null) {
contentBuffer.append(inputLine);
contentBuffer.append("/n");
}
contentBuffer.delete(contentBuffer.length() - 2, contentBuffer.length());
in.close();
return contentBuffer.toString();
}
}
测试:
public class Test {
public static void main(String[] args) throws Exception {
Shopemp shopemp = new Shopemp();
shopemp.setShop_num("210671");
shopemp.setEmp_num("D35778");
shopemp.setEmp_name("米亚南");
//shopemp.setOpt("D");
String result = OA.getResult("http://请求地址", appkey, 密钥, 方法名,shopemp );
//结果对象
GeneralResponse generalResponse = JSONObject.parseObject(result, GeneralResponse.class);
System.out.println("返回结果"+generalResponse.getIsSuccess()+"返回信息:"+generalResponse.getMessage());
}
}