核查医药代表备案信息是否正确

x33g5p2x  于2022-06-13 转载在 其他  
字(3.9k)|赞(0)|评价(0)|浏览(326)

一 背景

医药代表备案平台

首页-医药代表备案平台

该平台可通过备案号查询,也可通过药品上市许可持有人和医药代表姓名进行组合查询。

如果反复查询会禁用IP,每天查询信息量很少。

可通过 API 调用加伪造HTTP请求IP地址技术去实现大量药代备案信息查询。具体实现如下。

二 实现

1 HTTP工具类

package hospital;

import iputils.IPUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
* @className: HttpClientUtils
* @description: 本工具采用的是最新的 HttpComponents-Client-4.2.1 ,封装了一些采用 HttpClient 发送 HTTP 请求的方法
* @date: 2021/2/10
* @author: cakin
*/
public class HttpClientUtils {
    private static Log logger = LogFactory.getLog(hospital.HttpClientUtils.class);

    private HttpClientUtils() {
    }

    /**
     * 发送 HTTP_POST 请求
     *
     * @param url 请求地址
     * @return 返回的 JSON 字符串
     * @throws Exception
     */
    public static String sendPostByRandomIP(String url) throws Exception {
        CloseableHttpClient httpclient = HttpClients.custom().build();
        HttpPost post = null;
        String resData = null;
        CloseableHttpResponse result = null;
        try {
            post = new HttpPost(url);
            String randomIp = IPUtils.getRandomIp();
            post.setConfig(RequestConfig.custom().setConnectTimeout(30000).setSocketTimeout(30000).build());
            post.setHeader("Content-Type", "application/json");
            post.setHeader("X-Forwarded-For", randomIp);
            result = httpclient.execute(post);
            if (HttpStatus.SC_OK == result.getStatusLine().getStatusCode()) {
                resData = EntityUtils.toString(result.getEntity());
            }
        } finally {
            if (result != null) {
                result.close();
            }
            if (post != null) {
                post.releaseConnection();
            }
            httpclient.close();
        }
        return resData;
    }
}

2 IP工具类

package iputils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import hospital.HttpClientUtils;

import java.util.Random;

public class IPUtils {
    /**
     * 获取一个随机IP
     */
    public static String getRandomIp() {
        // 指定 IP 范围
        int[][] range = {
                {607649792, 608174079}, // 36.56.0.0-36.63.255.255
                {1038614528, 1039007743}, // 61.232.0.0-61.237.255.255
                {1783627776, 1784676351}, // 106.80.0.0-106.95.255.255
                {2035023872, 2035154943}, // 121.76.0.0-121.77.255.255
                {2078801920, 2079064063}, // 123.232.0.0-123.235.255.255
                {-1950089216, -1948778497}, // 139.196.0.0-139.215.255.255
                {-1425539072, -1425014785}, // 171.8.0.0-171.15.255.255
                {-1236271104, -1235419137}, // 182.80.0.0-182.92.255.255
                {-770113536, -768606209}, // 210.25.0.0-210.47.255.255
                {-569376768, -564133889}, // 222.16.0.0-222.95.255.255
        };

        Random random = new Random();
        int index = random.nextInt(10);
        String ip = num2ip(range[index][0] + random.nextInt(range[index][1] - range[index][0]));
        return ip;
    }

    /*
     * 将十进制转换成IP地址
     */
    public static String num2ip(int ip) {
        int[] b = new int[4];
        b[0] = (ip >> 24) & 0xff;
        b[1] = (ip >> 16) & 0xff;
        b[2] = (ip >> 8) & 0xff;
        b[3] = ip & 0xff;
        // 拼接 IP
        String x = b[0] + "." + b[1] + "." + b[2] + "." + b[3];
        return x;
    }

    public static void main(String[] args) {
        // 1 检查备案号是否为空,不为空则检查通过
        String realName = "梁XX";
        String orgName = "中国XX制药有限公司";
        // 从网站拉取备案号
        String url = "https://pharmareps.cpa.org.cn/record/selectcode?representativeName=" + realName + "&orgName=" + orgName;
        try {
            String res = HttpClientUtils.sendPostByRandomIP(url);
            JSONObject jsonObject = JSON.parseObject(res);
            Integer checkResult = jsonObject.getInteger("result");
            if (checkResult == 0) {
                JSONArray entityArray = jsonObject.getJSONArray("entity");
                // 备案号
                JSONObject entityObj = entityArray.getJSONObject(0);
                String representativeCode = entityObj.getString("code");
                System.out.println(representativeCode);
            } else {
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

三 测试

2020RU4W6XXX

相关文章