前言:该文章主要是总结我在实际工作中遇到的问题,在调取第三方系统的时候出现的问题,算自己的总结。各位博友如果有什么建议或意见欢迎留言指正。

  1. 先将准备传入参数
  2. 再与第三方系统建立连接
  3. 再第三方系统处理后给你返回需要的信息

调用第三方的系统的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;
}

java 调用第三方系统时的连接代码-记录的更多相关文章

  1. Java调用第三方http接口的方式

    1. 概述 在实际开发过程中,我们经常需要调用对方提供的接口或测试自己写的接口是否合适.很多项目都会封装规定好本身项目的接口规范,所以大多数需要去调用对方提供的接口或第三方接口(短信.天气等). 在J ...

  2. Java调用第三方dll文件的使用方法 System.load()或System.loadLibrary()

    Java调用第三方dll文件的使用方法 public class OtherAdapter { static { //System.loadLibrary("Connector") ...

  3. Java调用第三方接口示范

    在项目开发中经常会遇到调用第三方接口的情况,比如说调用第三方的天气预报接口. 使用流程[1]准备工作:在项目的工具包下导入HttpClientUtil这个工具类,或者也可以使用Spring框架的res ...

  4. java调用第三方的webservice应用实例

    互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示. 一些常用的webservice网站的链接地址: ...

  5. java调用第三方的webservice应用实例【转载】

    互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示. 一些常用的webservice网站的链接地址: ...

  6. 使用SAP open connector调用第三方系统的API

    我们把hubspot这个SaaS CRM作为第三方系统,首先登录hubspot,创建一个新的API key: 把创建的key拷贝到剪切板里: 然后登录SAP Cloud for Customer上的o ...

  7. 调用第三方库时需注意MD/MT的链接编译方式(遇到的坑记录)

    MD与/MT编译 1./MD是动态库链接方式编译 (DEBUG版本是/MDd) 2./MT是静态库链接方式编译 (DEBUG版本是/MTd) 编译器不会检查到的问题 我今天遇到的记录下来 当你调用第三 ...

  8. java 使用CXF将wsdl文件生成客户端代码命令java调用第三方的webservice应用实例

    1.先下载cxf包https://download.csdn.net/download/suizhikuo/108112362.解压缩包,通过cmd命令进入到bin目录下(cd cxf\bin的路径) ...

  9. wsdl 生成 java 代码 java 使用CXF将wsdl文件生成客户端代码命令java调用第三方的webservice应用实例 推荐使用, 并且设置了 utf8

    推荐使用, 并且设置了 utf8 wsdl2java -p cn.smborderservice  -encoding utf-8 -d f:\logink\src -all -autoNameRes ...

随机推荐

  1. Unity C# 运用 GetSaveFileName() 导出Excel文件

    本文原创,转载请注明出处:http://www.cnblogs.com/AdvancePikachu/p/6944870.html 唉哟,这次厉害咯,网上搜罗了好久,终于被我找到汉化的保存对话框了,根 ...

  2. C#数据库(MySQL)帮助类

    using MySql.Data.MySqlClient; using System; using System.Collections.Generic; using System.Configura ...

  3. 利用XML序列化和Asp.Net Web缓存实现站点配置文件

    我们经常会遇到这样的场景: 今天来了个业务,需要加一个字段,但是考虑的以后可能有变动,需要配成“活”的. 一般最初的做法就是加一个配置到Web.Config文件的AppSettings中去.但是这样有 ...

  4. iOS 基础笔试题

    参考:https://www.jianshu.com/p/1d3496bc5bda 1.#import 跟#include.@class有什么区别?#import<> 跟 #import& ...

  5. 可视化工具Navicat的使用/pymysql模块的使用

    一.可视化工具Navicat的使用 1.官网下载:http://www.navicat.com/en/products/navicat-for-mysql 2.网盘下载:http://pan.baid ...

  6. mysql登陆远程数据库

    1.登陆mysql 2.e mysql; 3.比如用户名密码为root/root. 你想root使用root从任何主机连接到mysql服务器的话. @’ ’后面加ip地址一般般为localhost或者 ...

  7. git学习(一)

    提:       远程的主机名(远程仓库服务器名):  origin   本地的主分支: master(本地master分支)      远程的主分支: maste(远程仓库的master分支) gi ...

  8. 常用宏OC

    #ifndef MacroDefinition_h #define MacroDefinition_h //-------------------获取设备大小--------------------- ...

  9. HDU5171 矩阵快速幂

    题目描述:http://acm.hdu.edu.cn/showproblem.php?pid=5171 算法: 可以先将数组a[]排序,然后序列 a1 , a2 , … , an 即为有序序列,则第一 ...

  10. c++指针二维数组

    ; int** G; //初始化 G = new int*[N]; ; i < N; i++) G[i] = new int[N]: //删除 ; i < N; i++) delete[] ...