Commit 851dab55 authored by 华润's avatar 华润

跨省单位开户鉴权认证请求第三方接口代码提交

parent 016f2ad0
...@@ -23,7 +23,7 @@ public class CacheDemoController { ...@@ -23,7 +23,7 @@ public class CacheDemoController {
* @return * @return
*/ */
@RequestMapping(value = "/saveToken", method = RequestMethod.GET) @RequestMapping(value = "/saveToken", method = RequestMethod.GET)
public String saveToken() { public static String saveToken() {
try { try {
//1.调用鉴权认证接口获取token值和过期时间的json字符串 //1.调用鉴权认证接口获取token值和过期时间的json字符串
String str = TokenAcquisitionController.TokenAcquisition(); String str = TokenAcquisitionController.TokenAcquisition();
...@@ -49,7 +49,7 @@ public class CacheDemoController { ...@@ -49,7 +49,7 @@ public class CacheDemoController {
* @return * @return
*/ */
@RequestMapping(value = "/getToken", method = RequestMethod.GET) @RequestMapping(value = "/getToken", method = RequestMethod.GET)
public String getToken(){ public static String getToken(){
try { try {
String token = TimeExpiredPoolCache.getInstance().get("access_token"); String token = TimeExpiredPoolCache.getInstance().get("access_token");
if (null!=token){ if (null!=token){
...@@ -61,5 +61,4 @@ public class CacheDemoController { ...@@ -61,5 +61,4 @@ public class CacheDemoController {
return null; return null;
} }
} }
...@@ -2,6 +2,7 @@ package net.cdkj.gjj.adapter.controller; ...@@ -2,6 +2,7 @@ package net.cdkj.gjj.adapter.controller;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import net.cdkj.gjj.adapter.domain.TimeExpiredPoolCache;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
...@@ -27,73 +28,79 @@ public class ProvidentFundServicesController { ...@@ -27,73 +28,79 @@ public class ProvidentFundServicesController {
@ResponseBody @ResponseBody
@PostMapping("ProvidentFundServices") @PostMapping("ProvidentFundServices")
public String ProvidentFundServices() { public String ProvidentFundServices() {
//1.调用鉴权认证判断token是否失效,若没失效则直接拿鉴权认证接口拿到的token值,若失效,则需要重新调鉴权认证接口获取新的token值才能调公积金系统服务接口 String token=null;
String resp = null; token=CacheDemoController.getToken();
JSONObject jsonObject = new JSONObject(); if(!"".equals(token)&&token!=null){//若token未过期,则用内存中的token
jsonObject.put("app_id", "111"); String resp = null;
jsonObject.put("app_secret", "222"); JSONObject jsonObject = new JSONObject();
jsonObject.put("grant_type", "127.0.0.1"); jsonObject.put("app_id", "111");
String str = jsonObject.toString(); jsonObject.put("app_secret", "222");
System.out.println("发送给第三方的报文:" + str); jsonObject.put("grant_type", "127.0.0.1");
StringBuffer sb = new StringBuffer(); String str = jsonObject.toString();
HttpURLConnection conn = null; System.out.println("发送给第三方的报文:" + str);
OutputStream out = null; StringBuffer sb = new StringBuffer();
try { HttpURLConnection conn = null;
// 创建url 资源 OutputStream out = null;
URL url = new URL("http://127.0.0.1:8080/FrontEndProcessor/FrontEndProcessor/ProvidentFundServices2");//测试第三方地址 try {
// 创建http 连接 // 创建url 资源
conn = (HttpURLConnection) url.openConnection(); URL url = new URL("http://127.0.0.1:8080/FrontEndProcessor/FrontEndProcessor/ProvidentFundServices2");//测试第三方地址
// 设置允许输出 // 创建http 连接
conn.setDoOutput(true); conn = (HttpURLConnection) url.openConnection();
// 设置允许输入 // 设置允许输出
conn.setDoInput(true); conn.setDoOutput(true);
// 设置不使用缓存 // 设置允许输入
conn.setUseCaches(false); conn.setDoInput(true);
// 设置传递方式 // 设置不使用缓存
conn.setRequestMethod("POST"); conn.setUseCaches(false);
// 设置维持长连接 // 设置传递方式
conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestMethod("POST");
// 设置文件类型: // 设置维持长连接
conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Connection", "Keep-Alive");
// 设置文件字符集: // 设置文件类型:
conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Content-Type", "application/json");
// 转换为字节数组 // 设置文件字符集:
byte[] data = (str).getBytes("UTF-8"); conn.setRequestProperty("Charset", "UTF-8");
// 设置文件长度 // 转换为字节数组
conn.setRequestProperty("Content-Length", String.valueOf(data.length)); byte[] data = (str).getBytes("UTF-8");
// 开始连接请求 // 设置文件长度
conn.connect(); conn.setRequestProperty("Content-Length", String.valueOf(data.length));
//创建写入流,写入请求的字符串 // 开始连接请求
out = new DataOutputStream(conn.getOutputStream()); conn.connect();
out.write(data); //创建写入流,写入请求的字符串
// 请求返回的状态 out = new DataOutputStream(conn.getOutputStream());
if (HttpURLConnection.HTTP_OK == conn.getResponseCode()) { out.write(data);
// 请求返回的数据 // 请求返回的状态
InputStream is = conn.getInputStream(); if (HttpURLConnection.HTTP_OK == conn.getResponseCode()) {
String readLine = new String(); // 请求返回的数据
BufferedReader responseReader = new BufferedReader(new InputStreamReader(is, "UTF-8")); InputStream is = conn.getInputStream();
while ((readLine = responseReader.readLine()) != null) { String readLine = new String();
sb.append(readLine); BufferedReader responseReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((readLine = responseReader.readLine()) != null) {
sb.append(readLine);
}
responseReader.close();
System.out.println("第三方返回的报文:" + sb.toString());
return sb.toString();
} else {
System.out.println("请求失败!!!");
} }
responseReader.close(); } catch (IOException e) {
System.out.println("第三方返回的报文:" + sb.toString()); e.printStackTrace();
return sb.toString(); } finally {
} else { if (out != null) {
System.out.println("请求失败!!!"); try {
} out.flush();
} catch (IOException e) { out.close();
e.printStackTrace(); } catch (IOException e) {
} finally { }
if (out != null) { }
try { if (conn != null) {
out.flush(); conn.disconnect();
out.close();
} catch (IOException e) {
} }
} }
if (conn != null) { }else{//若token已过期,则重新调用鉴权认证接口获取token存入内存中
conn.disconnect(); CacheDemoController.saveToken();
} token=CacheDemoController.getToken();
} }
return null; return null;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment