1.首先申请企查查账号和20次免费测试
2.可以下载企查查提供的demo
这里记录一下我自己的项目
pom.xml
<dependencies> <!--Hutool是一个小而全的Java工具类库--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.4.1</version> </dependency> <!-- httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.1</version> </dependency> </dependencies>
HttpHelper
package org.ld.httpGetDemo; import java.io.IOException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.cert.X509Certificate; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; 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; /** * 描述 http请求处理模块 */ public class HttpHelper { // 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) { System.out.println(String.format("http请求失败,uri{%s},exception{%s}", new Object[] { url, e })); } catch (IOException e) { System.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) { System.out.println(String.format("http请求失败,uri{%s},exception{%s}", new Object[] { url, e })); } catch (IOException e) { System.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 HttpHelper() { httpClientConnectionManager = new PoolingHttpClientConnectionManager(); httpClientConnectionManager.setMaxTotal(100); httpClientConnectionManager.setDefaultMaxPerRoute(20); } // get 请求 public static String httpGet(String url) throws Exception { return httpGet(url, null); } }
测试类
package org.ld.httpGetDemo; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.bejson.pojo.JsonRootBean; import org.apache.commons.codec.digest.DigestUtils; import org.apache.http.client.methods.HttpHead; /** * @author 陆赛赛 * 企查查API测试 * 测试结果: *1.keyword必填,可以是公司名、注册号或社会统一信用代码 * 2.pageIndex页码,默认第一页 * 3.pageSize每页条数,默认为10,最大不超过20条 * 4.keyword不能用城市名,会显示传入参数有误,请检查 * 5.一般数据量较大,常以千/万条为单位,分页查找较为麻烦 * 6.若要精确查找,需要填注册号或社会统一信用代码 */ public class MainApp { // 查看我的秘钥 我的Key private static final String appkey = "e003ad0571ec4623bde67753d81a6267"; private static final String seckey = "46432E9F056594D6DC10A712205FC2C3"; //公司名、注册号或社会统一信用代码 private static String keyword="恒信"; private static Integer pageIndex=null;//页码,默认第一页 private static Integer pageSize=null;//每页条数,默认为10,最大不超过20条 public static void main(String[] args) throws Exception { String jsonStr = createUrlForQiChaCha(keyword, pageIndex, pageSize); //1.使用工具类转换为JSONObject JSONObject jsonObject = JSONUtil.parseObj(jsonStr); //2.JSONObject对象转换为JavaBean JsonRootBean jsonRootBean = JSONUtil.toBean(jsonObject, JsonRootBean.class); System.out.println(jsonRootBean); } // 请求头配置 public 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; } //发送http请求,返回企查查开放接口的json字符串 public static String createUrlForQiChaCha(String keyword,Integer pageIndex,Integer pageSize) throws Exception { //默认 String reqInterNme = "http://api.qichacha.com/ECIV4/Search"; String paramStr = "keyword="+keyword; //请求头配置 HttpHead reqHeader = new HttpHead(); String[] autherHeader = RandomAuthentHeader(); reqHeader.setHeader("Token", autherHeader[0]); reqHeader.setHeader("Timespan", autherHeader[1]); //配置url String reqUri = reqInterNme.concat("?key=").concat(appkey).concat("&").concat(paramStr); //如果配置了页码和每页条数,url后加上下列参数 if(pageIndex!=null && pageSize!=null){ reqUri=reqUri+"&pageIndex="+pageIndex+"&pageSize="+pageSize; } //Get请求访问企查查开放接口 String tokenJson = HttpHelper.httpGet(reqUri, reqHeader.getAllHeaders()); return tokenJson; } }
api返回值封装成Javabean
package com.bejson.pojo; import java.util.List; public class JsonRootBean { private Paging Paging;//页码相关对象 private List<Result> Result;//返回数据 /* 101当前的KEY无效或者还未生效中 102当前KEY已欠费 103当前Key被暂停使用 104接口正在维护中 105接口已下线停用 107被禁止的IP或者签名错误 108请求格式错误,请重试 109请求超过系统限制 110当前相同查询连续出错,请等2小时后重试 111接口权限未开通 112您的账号剩余使用量已不足 113当前接口已被删除,请重新申请 114当前接口已被禁用,请联系管理员 115身份验证错误或者已过期 199系统未知错误,请联系技术客服 200查询成功 201查询无结果 202查询参数错误,请检查 203系统查询有异常,请联系技术人员 204请至少输入一个参数 205等待处理中 206无效的省份代码 207请求数据的条目数超过上限(5000) 208此接口不支持此公司类型查询 213参数长度不能小于2 214您还未购买过该接口,请先购买 215不支持的查询关键字 216国家代码不正确 221发票校验失败(参数与发票号码不对应) 222超过发票当日查验次数,请次日再试 299无效的请求 */ private String Status;//返回码 private String Message;//返回信息 private String OrderNumber;//订单号 public void setPaging(Paging Paging) { this.Paging = Paging; } public Paging getPaging() { return Paging; } public void setResult(List<Result> Result) { this.Result = Result; } public List<Result> getResult() { return Result; } public void setStatus(String Status) { this.Status = Status; } public String getStatus() { return Status; } public void setMessage(String Message) { this.Message = Message; } public String getMessage() { return Message; } public void setOrderNumber(String OrderNumber) { this.OrderNumber = OrderNumber; } public String getOrderNumber() { return OrderNumber; } @Override public String toString() { return "JsonRootBean{" + "Paging=" + Paging + ", Result=" + Result + ", Status='" + Status + '\'' + ", Message='" + Message + '\'' + ", OrderNumber='" + OrderNumber + '\'' + '}'; } }
package com.bejson.pojo; public class Paging { private int PageSize;//每页显示条数,默认为10,最多20 private int PageIndex;//页数,默认为1 private int TotalRecords;//总数量 public void setPageSize(int PageSize) { this.PageSize = PageSize; } public int getPageSize() { return PageSize; } public void setPageIndex(int PageIndex) { this.PageIndex = PageIndex; } public int getPageIndex() { return PageIndex; } public void setTotalRecords(int TotalRecords) { this.TotalRecords = TotalRecords; } public int getTotalRecords() { return TotalRecords; } @Override public String toString() { return "Paging{" + "PageSize=" + PageSize + ", PageIndex=" + PageIndex + ", TotalRecords=" + TotalRecords + '}'; } }
package com.bejson.pojo; import java.util.Date; public class Result { private String KeyNo;//内部KeyNo private String Name;//公司名称 private String OperName;//法人名称 private Date StartDate;//成立日期 private String Status;//企业状态 private String No;//注册号 private String CreditCode;//社会统一信用代码 public void setKeyNo(String KeyNo) { this.KeyNo = KeyNo; } public String getKeyNo() { return KeyNo; } public void setName(String Name) { this.Name = Name; } public String getName() { return Name; } public void setOperName(String OperName) { this.OperName = OperName; } public String getOperName() { return OperName; } public void setStartDate(Date StartDate) { this.StartDate = StartDate; } public Date getStartDate() { return StartDate; } public void setStatus(String Status) { this.Status = Status; } public String getStatus() { return Status; } public void setNo(String No) { this.No = No; } public String getNo() { return No; } public void setCreditCode(String CreditCode) { this.CreditCode = CreditCode; } public String getCreditCode() { return CreditCode; } @Override public String toString() { return "Result{" + "KeyNo='" + KeyNo + '\'' + ", Name='" + Name + '\'' + ", OperName='" + OperName + '\'' + ", StartDate=" + StartDate + ", Status='" + Status + '\'' + ", No='" + No + '\'' + ", CreditCode='" + CreditCode + '\'' + '}'; } }
执行main方法就可以了