Commit ff478c7b authored by 张欣's avatar 张欣

对接企查查接口

parent 6325a97b
package com.mth.requestsecret.controller;
import com.mth.requestsecret.util.QccHttpUtil;
import com.mth.requestsecret.util.QccUtil;
import org.apache.http.client.methods.HttpHead;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
/**
* 企查查接口
*
* @author zx
* @date 2020/8/18
*/
@RestController
public class QccController {
// 查看我的秘钥 我的Key
private static final String appkey = "57bdcf2784834ff6a96ec3168184c012";
private static final String seckey = "96C7DF69FCF5D4C170844D81C8074003";
private Logger log = LoggerFactory.getLogger(QccController.class);
/**
* 工商企业信息模糊查询
* @param keyword
* @return
*/
@GetMapping("/qiyeSearch")
public String qiyeSearch(@RequestParam String keyword) throws Exception {
String interfaceUrl = "http://api.qichacha.com/ECIV4/Search";
HttpHead reqHeader = new HttpHead();
String[] autherHeader = QccUtil.RandomAuthentHeader(appkey,seckey);
reqHeader.setHeader("Token", autherHeader[0]);
reqHeader.setHeader("Timespan", autherHeader[1]);
String reqUri = interfaceUrl.concat("?key=").concat(appkey).concat("&keyword=").concat(keyword);
String resultJson = QccHttpUtil.httpGet(reqUri, reqHeader.getAllHeaders());
log.info("企查查企业查询接口返回结果为:"+resultJson);
return resultJson;
}
/**
* 企业股东信息查询
* @param keyword
* @return
*/
@GetMapping("/qiyePartnerInfoQuery")
public String qiyePartnerInfoQuery(@RequestParam String keyword) throws Exception {
String interfaceUrl = "http://api.qichacha.com/ECIPartner/GetList";
HttpHead reqHeader = new HttpHead();
String[] autherHeader = QccUtil.RandomAuthentHeader(appkey,seckey);
reqHeader.setHeader("Token", autherHeader[0]);
reqHeader.setHeader("Timespan", autherHeader[1]);
String reqUri = interfaceUrl.concat("?key=").concat(appkey).concat("&searchKey=").concat(keyword);
String resultJson = QccHttpUtil.httpGet(reqUri, reqHeader.getAllHeaders());
log.info("企查查企业股东信息查询接口返回结果为:"+resultJson);
return resultJson;
}
/**
* 个人历史担任法人企业查询
* @param searchKey 公司名、注册号、社会统一信用 代码,KeyNo
* @param personName 人员姓名
* @return
*/
@GetMapping("/personHisOperCompany")
public String personHisOperCompany(@RequestParam String searchKey,@RequestParam String personName) throws Exception {
String interfaceUrl = "http://api.qichacha.com/PersonHisOperCompany/GetList";
HttpHead reqHeader = new HttpHead();
String[] autherHeader = QccUtil.RandomAuthentHeader(appkey,seckey);
reqHeader.setHeader("Token", autherHeader[0]);
reqHeader.setHeader("Timespan", autherHeader[1]);
String reqUri = interfaceUrl.concat("?key=").concat(appkey).concat("&searchKey=").concat(searchKey).concat("&personName=").concat(personName);
String resultJson = QccHttpUtil.httpGet(reqUri, reqHeader.getAllHeaders());
log.info("个人历史担任法人企业查询接口返回结果为:"+resultJson);
return resultJson;
}
}
package com.mth.requestsecret.util;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
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.methods.HttpUriRequest;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import static java.lang.System.out;
public class QccHttpUtil {
// get 请求
public static String httpGet(String url, Header[] headers) throws Exception {
HttpUriRequest uriRequest = new HttpGet(url);
if (null != headers)
uriRequest.setHeaders(headers);
CloseableHttpClient httpClient = null;
try {
httpClient = declareHttpClientSSL(url);
CloseableHttpResponse httpresponse = httpClient.execute(uriRequest);
HttpEntity httpEntity = httpresponse.getEntity();
String result = EntityUtils.toString(httpEntity, REQ_ENCODEING_UTF8);
return result;
} catch (ClientProtocolException e) {
out.println(String.format("http请求失败,uri{%s},exception{%s}", new Object[] { url, e }));
} catch (IOException e) {
out.println(String.format("IO Exception,uri{%s},exception{%s}", new Object[] { url, e }));
} finally {
if (null != httpClient)
httpClient.close();
}
return null;
}
// post 请求
public static String httpPost(String url, String params) throws Exception {
HttpPost post = new HttpPost(url);
post.addHeader("Content-Type", "application/json;charset=" + REQ_ENCODEING_UTF8);
// 设置传输编码格式
StringEntity stringEntity = new StringEntity(params, REQ_ENCODEING_UTF8);
stringEntity.setContentEncoding(REQ_ENCODEING_UTF8);
post.setEntity(stringEntity);
HttpResponse httpresponse = null;
CloseableHttpClient httpClient = null;
try {
httpClient = declareHttpClientSSL(url);
httpresponse = httpClient.execute(post);
HttpEntity httpEntity = httpresponse.getEntity();
String result = EntityUtils.toString(httpEntity, REQ_ENCODEING_UTF8);
return result;
} catch (ClientProtocolException e) {
out.println(String.format("http请求失败,uri{%s},exception{%s}", new Object[] { url, e }));
} catch (IOException e) {
out.println(String.format("IO Exception,uri{%s},exception{%s}", new Object[] { url, e }));
} finally {
if (null != httpClient)
httpClient.close();
}
return null;
}
private static CloseableHttpClient declareHttpClientSSL(String url) {
if (url.startsWith("https://")) {
return sslClient();
} else {
return HttpClientBuilder.create().setConnectionManager(httpClientConnectionManager).build();
}
}
/**
* 设置SSL请求处理
*/
private static CloseableHttpClient sslClient() {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] xcs, String str) {
}
public void checkServerTrusted(X509Certificate[] xcs, String str) {
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLConnectionSocketFactory sslConnectionSocketFactory = SSLConnectionSocketFactory.getSocketFactory();
return HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).build();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (KeyManagementException e) {
throw new RuntimeException(e);
}
}
// this is config
private static final String REQ_ENCODEING_UTF8 = "utf-8";
private static PoolingHttpClientConnectionManager httpClientConnectionManager;
public QccHttpUtil() {
httpClientConnectionManager = new PoolingHttpClientConnectionManager();
httpClientConnectionManager.setMaxTotal(100);
httpClientConnectionManager.setDefaultMaxPerRoute(20);
}
// get 请求
public static String httpGet(String url) throws Exception {
return httpGet(url, null);
}
}
package com.mth.requestsecret.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.client.methods.HttpHead;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.regex.Pattern;
import static java.lang.System.out;
public class QccTestMain {
// 查看我的秘钥 我的Key
private static final String appkey = "57bdcf2784834ff6a96ec3168184c012";
private static final String seckey = "96C7DF69FCF5D4C170844D81C8074003";
// 获取返回码 Res Code
static class HttpCodeRegex {
private static final String ABNORMAL_REGIX = "(101)|(102)";
private static final Pattern pattern = Pattern.compile(ABNORMAL_REGIX);
protected static boolean isAbnornalRequest(final String status) {
return pattern.matcher(status).matches();
}
}
// 获取Auth Code
protected static final String[] RandomAuthentHeader() {
String timeSpan = String.valueOf(System.currentTimeMillis() / 1000);
String[] authentHeaders = new String[] { DigestUtils.md5Hex(appkey.concat(timeSpan).concat(seckey)).toUpperCase(), timeSpan };
return authentHeaders;
}
// 解析JSON
protected static String FormartJson(String jsonString, String key) throws JSONException {
JSONObject jObject = new JSONObject(jsonString);
return (String) jObject.get(key);
}
// pretty print 返回值
protected static void PrettyPrintJson(String jsonString) throws JSONException {
try {
ObjectMapper mapper = new ObjectMapper();
Object obj = mapper.readValue(jsonString, Object.class);
String indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
out.println(indented);
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String reqInterNme = "http://api.qichacha.com/ECIV4/Search";
String paramStr = "keyword=浙江华通云数据科技有限公司";
String status = "";
try {
// auth header setting
HttpHead reqHeader = new HttpHead();
String[] autherHeader = RandomAuthentHeader();
reqHeader.setHeader("Token", autherHeader[0]);
reqHeader.setHeader("Timespan", autherHeader[1]);
final String reqUri = reqInterNme.concat("?key=").concat(appkey).concat("&").concat(paramStr);
String tokenJson = QccHttpUtil.httpGet(reqUri, reqHeader.getAllHeaders());
out.println(String.format("==========================>this is response:{%s}", tokenJson));
// parse status from json
status = FormartJson(tokenJson, "Status");
out.println(String.format("==========================>Status:{%s}", status));
if (!com.mth.requestsecret.util.QccTestMain.HttpCodeRegex.isAbnornalRequest(status)) {
PrettyPrintJson(tokenJson);
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
package com.mth.requestsecret.util;
;
import org.apache.commons.codec.digest.DigestUtils;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.regex.Pattern;
import static java.lang.System.out;
public class QccUtil {
// 获取返回码 Res Code
static class HttpCodeRegex {
private static final String ABNORMAL_REGIX = "(101)|(102)";
private static final Pattern pattern = Pattern.compile(ABNORMAL_REGIX);
protected static boolean isAbnornalRequest(final String status) {
return pattern.matcher(status).matches();
}
}
// 获取Auth Code
public static final String[] RandomAuthentHeader(String appkey,String seckey) {
String timeSpan = String.valueOf(System.currentTimeMillis() / 1000);
String[] authentHeaders = new String[] { DigestUtils.md5Hex(appkey.concat(timeSpan).concat(seckey)).toUpperCase(), timeSpan };
return authentHeaders;
}
// 解析JSON
public static String FormartJson(String jsonString, String key) throws JSONException {
JSONObject jObject = new JSONObject(jsonString);
return (String) jObject.get(key);
}
// pretty print 返回值
// public static void PrettyPrintJson(String jsonString) throws JSONException {
// try {
// ObjectMapper mapper = new ObjectMapper();
// Object obj = mapper.readValue(jsonString, Object.class);
// String indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
// out.println(indented);
// } catch (JsonProcessingException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
}
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