栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java企业工商信息查询

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java企业工商信息查询

企业工商信息查询

  public static void getBusiness(){
        String appId = "数脉分配给你的appid";
        String appSecurity = "数脉分配给你的appsecurity";
        String timestamp = System.currentTimeMillis()+"";
        //参数
        String keyword = "关键字,公司全名称、注册号、社会统一信用代码";
        //接口地址
        String url = "https://api.shumaidata.com/v4/business2/get";
        //请下载MD5Utils文件,地址为 https://file.tianyandata.cn/demo/utils/MD5Utils.java
        String sign = MD5Utils.encrypt(appId+"&"+timestamp+"&"+appSecurity);
        System.out.println("sign: "+sign);
        HashMap params = new HashMap<>();
        params.put("appid", appId);
        params.put("timestamp", timestamp);
        params.put("sign", sign);
        params.put("keyword", keyword);
        //请下载HttpUtils文件,地址为https://file.tianyandata.cn/demo/utils/HttpUtils.java
        String result = HttpUtils.get(url, null, params);
        System.out.println(result);
        
    }

HttpUtils

package com.shumai.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class HttpUtils {

    private static final String DEFAULT_CHARSET = "UTF-8";


    private static PoolingHttpClientConnectionManager connectionManager;

    private static HttpClient client;

    private HttpUtils() {
    }

    static {
        try {
            SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
            SSLConnectionSocketFactory sslSf = new SSLConnectionSocketFactory(sslContext,
                    SSLConnectionSocketFactory.getDefaultHostnameVerifier());
            Registry socketFactoryRegistry =
                    RegistryBuilder.create().register("https", sslSf).register("http",
                            PlainConnectionSocketFactory.getSocketFactory()).build();
            connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
            connectionManager.setMaxTotal(50);
            connectionManager.setDefaultMaxPerRoute(25);
            client = getSSLHttpClient();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static CloseableHttpClient getHttpClient() {
        return HttpClients.custom().setConnectionManager(connectionManager).build();
    }

    public static CloseableHttpClient getSSLHttpClient() {
        // 浣跨敤TrustSelfSignedStrategy瀹硅Self Signed鐨勮瘉涔�
        SSLContext sslContext = null;
        try {
            sslContext = SSLContextBuilder.create().loadTrustMaterial(new TrustSelfSignedStrategy()).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 涓嶅仛鏈嶅姟鍣ㄥ悕楠岃瘉锛岃繖鏄彲閫夌殑, 濡傛灉闇€瑕佸鏈嶅姟鍣ㄥ悕鍋氶獙璇侊紝鍙互鍘绘帀璇ラ厤缃�
        HostnameVerifier allowAllHosts = new NoopHostnameVerifier();
        // 浣跨敤SSLContext鍒涘缓SSL Socket Factory
        SSLConnectionSocketFactory connectionFactory = new SSLConnectionSocketFactory(sslContext, allowAllHosts);
        // 浣跨敤HttpClient Factory鍒涘缓HttpClient
        return HttpClients.custom().setSSLSocketFactory(connectionFactory).build();
    }

    
    public static String get(String uri, Map headers, Map data) {
        HttpClient client = getHttpClient();
        List params = new ArrayList<>();
        if (data != null) {
            for(Map.Entry entry : data.entrySet()) {
                params.add(new BasicNameValuePair(entry.getKey() + "", entry.getValue() + ""));
            }
        }
        try {
            String str = EntityUtils.toString(new UrlEncodedFormEntity(params, DEFAULT_CHARSET));
            HttpGet get = new HttpGet(uri + "?" + str);
            if(headers != null) {
                for(Map.Entry entry : headers.entrySet()) {
                    get.addHeader(entry.getKey(), entry.getValue());
                }
            }
            HttpResponse response = client.execute(get);
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity, "UTF-8").trim();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    
    public static String postForm(String uri, Map headers, Map data) {
        HttpClient client = getHttpClient();
        HttpPost post = new HttpPost(uri);
        List form = new ArrayList<>();
        for(Map.Entry entry : data.entrySet()) {
            form.add(new BasicNameValuePair(entry.getKey() + "", entry.getValue() + ""));
        }
        if(headers != null) {
            for(Map.Entry entry : headers.entrySet()) {
                post.addHeader(entry.getKey(), entry.getValue());
            }
        }
        try {
            post.setEntity(new UrlEncodedFormEntity(form, DEFAULT_CHARSET));
            HttpResponse response = client.execute(post);
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity, "UTF-8").trim();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    
    public static String postData(String uri, Map headers, String content) {
        HttpClient client = getHttpClient();
        HttpPost post = new HttpPost(uri);
        post.setEntity(new StringEntity(content, DEFAULT_CHARSET));
        if(headers != null) {
            for(Map.Entry entry : headers.entrySet()) {
                post.addHeader(entry.getKey(), entry.getValue());
            }
        }
        return postCommon(client, post);
    }



    
    public static String postSSLData(String uri, Map headers, String content) {
        HttpPost post = new HttpPost(uri);
        post.setEntity(new StringEntity(content, DEFAULT_CHARSET));
        if(headers != null) {
            for(Map.Entry entry : headers.entrySet()) {
                post.addHeader(entry.getKey(), entry.getValue());
            }
        }
        return postCommon(client, post);
    }

    private static String postCommon(HttpClient client, HttpPost post) {
        try {
            HttpResponse response = client.execute(post);
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity, "UTF-8").trim();
        } catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1027907.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号