OuterInterfaceUtil类
package com.dsk.trader.order.web.station.service.impl;
import com.alibaba.fastjson.JSON;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
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.springframework.util.CollectionUtils;
/**
* 描述:JAVA API对接外部接口标准参考
*
* @author wanglh
* @create 2021年03月09日 11:01
*/
@Slf4j
public class OuterInterfaceUtil {
/**
* @Author wanglh
* @Description 带参数的get请求
* @Date 2021/3/9 11:15
*/
public static String doGet(String url, Map<String, String> params) {
String result = "";
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = null;
try {
URIBuilder builder = new URIBuilder(url);
if (!CollectionUtils.isEmpty(params)) {
for (String key : params.keySet()) {
builder.addParameter(key, params.get(key));
}
}
URI uri = builder.build();
//创建http get请求
HttpGet httpGet = new HttpGet(uri);
log.info("-----------------请求路径----------------" + httpGet.getURI());
// Header[] allHeaders = httpGet.getAllHeaders();
// System.out.println(allHeaders.length);
// for (Header header : allHeaders) {
// System.out.println("-----------------请求头元素----------------" +header.getElements());
// }
log.info("-----------------请求方式-----------------" + httpGet.getMethod());
log.info("-----------------请求方式-----------------" + httpGet.getRequestLine().getMethod());
log.info("-----------------通信协议及版本-----------------" + httpGet.getRequestLine().getProtocolVersion());
log.info("-----------------请求路径-----------------" + httpGet.getRequestLine().getUri());
//执行请求
httpResponse = httpClient.execute(httpGet);
StatusLine statusLine = httpResponse.getStatusLine();
// 判断返回状态是否为200
if (statusLine != null && 200 == statusLine.getStatusCode()) {
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity, "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
closeIO(httpClient, httpResponse);
}
return result;
}
/**
* @Author wanglh
* @Description 不带参数的get请求
* @Date 2021/3/9 11:16
*/
public static String doGet(String url) {
return doGet(url, null);
}
/**
* @Author wanglh
* @Description 带参数的post请求
* @Date 2021/3/9 11:19
*/
public static String doPost(String url, Map<String, String> params) {
String result = "";
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = null;
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
if (!CollectionUtils.isEmpty(params)) {
// 创建参数列表
List<NameValuePair> paramList = new ArrayList<>();
for (String key : params.keySet()) {
// 装配参数列表
paramList.add(new BasicNameValuePair(key, params.get(key)));
}
// 模拟表单
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(paramList);
httpPost.setEntity(formEntity);
}
log.info("-----------------请求路径-----------------" + httpPost.getURI());
log.info("-----------------httpPost.getAllHeaders()-----------------" + httpPost.getAllHeaders());
log.info("-----------------请求方式-----------------" + httpPost.getMethod());
log.info("-----------------请求方式-----------------" + httpPost.getRequestLine().getMethod());
log.info("-----------------通信协议版本-----------------" + httpPost.getRequestLine().getProtocolVersion());
log.info("-----------------请求路径-----------------" + httpPost.getRequestLine().getUri());
//执行http请求
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity, "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
closeIO(httpClient, httpResponse);
}
return result;
}
/**
* @Author wanglh
* @Description 不带参数的post请求
* @Date 2021/3/9 11:33
*/
public static String doPost(String url) {
return doPost(url, null);
}
/**
* @Author wanglh
* @Description 传送json类型的post请求
* @Date 2021/3/9 11:34
*/
public static String doPostJson(String url, String json) {
String result = "";
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = null;
try {
HttpPost httpPost = new HttpPost(url);
StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(stringEntity);
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity, "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
closeIO(httpClient, httpResponse);
}
return result;
}
/**
* @Author wanglh
* @Description 测试查询身份证号码,多次测试会出现报错情况,可停顿2分钟再调用,未细究 可参考此网站:http://www.dffyw.com/sfzcx/
* @Date 2021/3/9 11:43
*/
public static String getIdentityResult(Map<String, String> params) {
String result = "";
log.info("-----------------查询身份证号码API开始-----------------");
String url = "http://www.dffyw.com/sfzcx/query.php?";
try {
result = OuterInterfaceUtil.doGet(url, params);
log.info("-----------------请求结果-----------------" + result);
} catch (Exception e) {
log.info("*******************查询身份证号码异常*******************");
e.printStackTrace();
}
log.info("-----------------查询身份证号码API结束-----------------");
return result;
}
public static void main(String[] args) {
Map<String, String> params = new HashMap<>();
params.put("id", "341621198503222135");
// params.put("WebShieldSessionVerify","NQtdMvu2FrFe5UwYOa90");
String result = OuterInterfaceUtil.getIdentityResult(params);
//直接输出会出现Unicode编码格式问题 百度搜索 "Gender":"\u7537"Unicode与中文互转,
//{"InputID":"341621198503222135","OutputID":"341621198503222132","Birthday":"1985\u5e7403\u670822\u65e5","Gender":"\u7537",
// "Address":"\u5b89\u5fbd\u7701-\u4eb3\u5dde\u5e02-\u6da1\u9633\u53bf","Error":"","Warning":"\u68c0\u9a8c\u9519\u8bef"}
// System.out.println("===直接输出==="+result);
Object parse = JSON.parse(result);
User user = JSON.parseObject(result, User.class);
System.out.println("============转换为json============" + parse);
System.out.println("============转换为对象============" + user);
}
public static void closeIO(CloseableHttpClient httpClient, CloseableHttpResponse httpResponse) {
try {
if (httpResponse != null) {
httpResponse.close();
}
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
User实体类
package com.dsk.trader.order.web.station.service.impl;
import java.util.Date;
import lombok.Data;
/**
* 描述:
*
* @author wanglihong
* @create 2021年03月09日 13:54
*/
@Data
public class User {
private String address;
private Date birthday;
private String error;
private String gender;
private String inputID;
private String outputID;
private String warning;
}