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

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

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