JAVA对接第三方接口

📅 2026/7/20 7:16:23
JAVA对接第三方接口
对接第三方接口是程序开发中的常见操作。下面是JAVA语言写的对接demoimport org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; 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.json.JSONObject; import java.util.ArrayList; import java.util.List; /** * 开放平台接口调用示例 * 修复资源释放、泛型、异常处理、连接池、参数校验、状态码判断 */ public class GovOpenApiClient { // 统一字符集常量便于统一修改 private static final String CHARSET_GBK GBK; // 全局单例HttpClient带连接池复用TCP连接 private static final CloseableHttpClient HTTP_CLIENT; private static final RequestConfig REQUEST_CONFIG; static { // 全局超时配置 REQUEST_CONFIG RequestConfig.custom() .setConnectTimeout(300 * 1000) .setSocketTimeout(300 * 1000) .build(); // 创建带连接池的http客户端全局复用 HTTP_CLIENT HttpClients.createDefault(); } public static void main(String[] args) { // 1. 业务参数实际项目放入yml/properties配置 String appkey ; String secret ; String nlid ; String sxid ; String apiUrl http://127.0.0.1:8086/scspsp/xxxx; // 2. 组装业务JSON报文 JSONObject businessJson new JSONObject(); businessJson.put(sfzhm, 370101000000000000); businessJson.put(姓名, 张三); try { // 3. 前置参数非空校验 if (appkey.isBlank() || secret.isBlank() || nlid.isBlank() || sxid.isBlank()) { System.err.println(关键配置参数不能为空); return; } // 生成签名、加密业务报文 String sign ScsUtil.createSign(secret, businessJson); String parastr ScsUtil.encryptMode(businessJson.toString()); if (sign.isBlank() || parastr.isBlank()) { System.err.println(签名或业务报文加密失败); return; } // 4. 构造表单参数规范泛型 ListBasicNameValuePair formParams new ArrayList(); formParams.add(new BasicNameValuePair(appkey, appkey)); // 安全优化secret不传递到接口仅本地用于生成签名 formParams.add(new BasicNameValuePair(sign, sign)); formParams.add(new BasicNameValuePair(nlid, nlid)); formParams.add(new BasicNameValuePair(sxid, sxid)); formParams.add(new BasicNameValuePair(parastr, parastr)); // 5. 发起POST请求 HttpPost httpPost new HttpPost(apiUrl); httpPost.setConfig(REQUEST_CONFIG); HttpEntity formEntity new UrlEncodedFormEntity(formParams, CHARSET_GBK); httpPost.setEntity(formEntity); // 7. try-with-resources 自动关闭流杜绝资源泄漏 try (CloseableHttpResponse response HTTP_CLIENT.execute(httpPost)) { HttpEntity respEntity response.getEntity(); if (respEntity null) { System.err.println(接口返回空响应); return; } // 判断HTTP状态码 int statusCode response.getStatusLine().getStatusCode(); String respStr EntityUtils.toString(respEntity, CHARSET_GBK); // 打印日志便于排查 System.out.println(请求地址 apiUrl); System.out.println(响应状态码 statusCode); System.out.println(接口返回报文 respStr); if (statusCode ! 200) { System.err.println(接口调用失败非200响应码); return; } // 业务报文解析逻辑... } finally { // 确保消耗流释放资源 EntityUtils.consume(formEntity); } } catch (Exception e) { // 细分异常可捕获SocketTimeoutException、ConnectException等单独处理 System.err.println(接口调用发生异常 e.getMessage()); e.printStackTrace(); } } }