java 调用第三方系统时的连接代码-记录
前言:该文章主要是总结我在实际工作中遇到的问题,在调取第三方系统的时候出现的问题,算自己的总结。各位博友如果有什么建议或意见欢迎留言指正。
- 先将准备传入参数
- 再与第三方系统建立连接
- 再第三方系统处理后给你返回需要的信息
调用第三方的系统的url(restful风格)
http://Ip地址:端口号/casereview/api/addTCaseReview
在业务逻辑实现类中通过工具类调用第三方接口。
String postParam = “”; //传入参数
String url = “”; //第三方接口url
long timeOut = 20000; //超时时间
JSONObject response = CaseEvaluationUtil.sendPost(postParam, url, timeOut );
//可以通过读取配置文件获取第三方接口地址
private static final String EVALUATION_IP = (String) ConcurrentCache.getFieldValue(“evaluationIp”);
通过工具类进行与第三方接口连接交互。
输出的参数
下面代码中设置请求头的信息来源于Request Headers
/**
* @description 向合议庭评议系统发送post请求
* @author junbao
* @create 2017年11月13日下午3:22:56
* @version 1.0
* @param postParam
* @param url
* @return String
* @throws BusinessErrorException
*/
public static JSONObject sendPost(String postParam, String url, int timeout) throws BusinessErrorException {
JSONObject result = new JSONObject();
PrintWriter out = null;
BufferedReader in = null;
try {
if (StringUtils.isBlank(EVALUATION_IP)) {
throw new BusinessErrorException(CommonConstants.FAILURE, "EVALUATION_IP 不能为空");
}
String targetURL = "http://" + EVALUATION_IP + url;
URL resetServiceURL = new URL(targetURL);
//打开url连接
HttpURLConnection httpConnection = (HttpURLConnection) resetServiceURL.openConnection();
//设置连接请求头信息属性
httpConnection.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
httpConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
httpConnection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9");
httpConnection.setRequestProperty("Connection", "keep-alive");
httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
httpConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36");
httpConnection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
//设置超时时间
httpConnection.setConnectTimeout(timeout);
httpConnection.setReadTimeout(timeout);
//设置请求方式
httpConnection.setRequestMethod("POST");
httpConnection.setDoInput(true);
httpConnection.setDoOutput(true);
//POST请求不应该使用cache
httpConnection.setUseCaches(false);
//获取url连接
httpConnection.connect();
//获取UrlConnection对象的输出流,调用httpConnection.getOutputStream的时候就会设置为POST方法
out = new PrintWriter(new OutputStreamWriter(httpConnection.getOutputStream(),"UTF-8"));
out.write(postParam);
//flush输出流的缓冲,将参数发送出去
out.flush();
//表示连接异常
if (httpConnection.getResponseCode() != 200) {
result.put("message", "数据获取异常,请联系系统管理员");
result.put("result", "ERROR");
result.put("code", null);
result.put("object", null);
return result;
}
//读取流中的内容
in = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(), "UTF-8"));
String line = "";
StringBuffer bf = new StringBuffer();
while (null !=(line = in.readLine())) {
bf.append(line);
}
if (StringUtils.isNoneBlank(bf.toString())) {
JSONObject evalutionResult = JSONObject.parseObject(bf.toString());
return evalutionResult;
}
} catch (Exception e) {
logger.error("Send post Exection!",e);
result.put("message", "数据获取异常,请联系系统管理员");
result.put("result", "ERROR");
result.put("code", null);
result.put("object", null);
return result;
} finally {
// 关闭流
try {
if (null != out) {
out.close();
}
if (null != in) {
in.close();
}
} catch (Exception e) {
logger.info("Send post Exection!");
e.printStackTrace();
result.put("message", "数据获取异常,请联系系统管理员");
result.put("result", "ERROR");
result.put("code", null);
result.put("object", null);
return result;
}
}
return result;
}