Java 查询企业基本信息接口实现(企查查)
因项目需要,系统中需要添加根据企业名称或统一信用代码查询企业信息的功能,所以整合了企查查的查询接口
接口文档地址
Java请求示例:
/** * 版权申明: 苏州朗动科技有限公司<br> * 项目描述: 企查查-接口平台<br> * 该接口调用demo仅供学习参考 */
package org.ld.httpGetDemo;
import static java.lang.System.out;
import java.io.IOException;
import java.util.regex.Pattern;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.client.methods.HttpHead;
import org.json.JSONException;
import org.json.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/** * * 描述 http请求处理模块<br> * * @author szld<br> * @version 1.0 <br> * 日期:2019年7月31日 下午2:23:41 */
public class MainApp {
// 请登录http://yjapi.com/DataCenter/MyData
// 查看我的秘钥 我的Key
private static final String appkey = "我的接口:我的Key";
private static final String seckey = "我的接口:我的秘钥";
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 = HttpHelper.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 (!HttpCodeRegex.isAbnornalRequest(status)) {
PrettyPrintJson(tokenJson);
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
// 获取返回码 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();
}
}
}
我将代码稍微改造了一下,仅供参考,如有错误,劳您指出:
@Value("${qichacha.key}")
private String key;
@Value("${qichacha.secret}")
private String secret;
//搜索关键字(公司名、注册号、社会统一信用代码或KeyNo)注:社会组织、香港企业仅支持通过企业名称和KeyNo查询
@Log("企业信息列表")
@ApiOperation(value = "企业信息列表")
@GetMapping(value = "list.do" , produces = "application/json;charset=UTF-8")
public R pageList(@RequestParam(value = "keyword")String keyword ,HttpServletRequest request) {
List<EnterpriseInfoDO> resultList = new ArrayList<>();
String reqInterNme = "http://api.qichacha.com/ECIV4/Search";
String paramStr = "keyword="+keyword;
try {
// auth header setting
HttpHead reqHeader = new HttpHead();
String[] autherHeader = QiChaChaUtil.RandomAuthentHeader(key,secret);
reqHeader.setHeader("Token", autherHeader[0]);
reqHeader.setHeader("Timespan", autherHeader[1]);
final String reqUri = reqInterNme.concat("?key=").concat(key).concat("&").concat(paramStr).concat("&pageSize=20");
String tokenJson = HttpHelper.httpGet(reqUri, reqHeader.getAllHeaders());
JSONObject jsonObject = JSON.parseObject(tokenJson);
return R.ok().put("result",resultList);
} catch (Exception e1) {
e1.printStackTrace();
return R.error("查询企业信息失败");
}
}
}
企查查工具类
import static java.lang.System.out;
import java.io.IOException;
import java.util.regex.Pattern;
import org.apache.commons.codec.digest.DigestUtils;
import org.json.JSONException;
import org.json.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class QiChaChaUtil {
// 获取返回码 Res Code
public 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();
}
}
}
application.yml
key和密钥登录企查查
建议把查询出的数据持久化到数据库,如有相同查询 直接从数据库返回数据。
还没有评论,来说两句吧...