如何用遍历的方法依照API查询手机号

实验题,已知犯罪嫌疑人的手机号码前三位为135,后两位为15,并且拥有API的示例代码与手机号归属地,如何通过通过遍历手机号中的4-7位,即一万个数据,以所属地区为筛选条件进一步缩小范围?(下面是API示例代码)


 
import com.baidubce.http.ApiExplorerClient;
import com.baidubce.http.AppSigner;
import com.baidubce.http.HttpMethodName;
import com.baidubce.model.ApiExplorerRequest;
import com.baidubce.model.ApiExplorerResponse;
 
// 号码归属查询api Java示例代码
public class RequestDemo {
    public static void main(String[] args) {
        String path = "http://gsd.api.bdymkt.com/sms";
        ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, path);
        request.setCredentials("您的 access key", "您的 secret key");
 
        request.addHeaderParameter("Content-Type", "application/json;charset=UTF-8");
        
        request.addQueryParameter("mobile", "");
        
        String requestExample = "\r\nimport com.baidubce.http.ApiExplorerClient;\r\nimport com.baidubce.http.AppSigner;\r\nimport com.baidubce.http.HttpMethodName;\r\nimport com.baidubce.model.ApiExplorerRequest;\r\nimport com.baidubce.model.ApiExplorerResponse;\r\n\r\n\/\/ 号码归属查询api Java示例代码\r\npublic class RequestDemo {\r\n    public static void main(String[] args) {\r\n        String path = \"http:\/\/gsd.api.bdymkt.comhttp:\/\/gwgp-g8eennmvmcz.n.bdcloudapi.com\/sms\";\r\n        ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, path);\r\n        request.setCredentials(\"您的 access key\", \"您的 secret key\");\r\n\r\n        request.addHeaderParameter(\"Content-Type\", \"application\/json;charset=UTF-8\");\r\n        \r\n        request.addQueryParameter(\"mobile\", \"\");\r\n        \r\n        \r\n\r\n        ApiExplorerClient client = new ApiExplorerClient(new AppSigner());\r\n\r\n        try {\r\n          ApiExplorerResponse response = client.sendRequest(request);\r\n          \/\/ 返回结果格式为Json字符串\r\n          System.out.println(response.getResult());\r\n        } catch (Exception e) {\r\n          e.printStackTrace();\r\n        }\r\n    }\r\n}";
        request.setJsonBody(requestExample);
        
 
        ApiExplorerClient client = new ApiExplorerClient(new AppSigner());
 
        try {
          ApiExplorerResponse response = client.sendRequest(request);
          // 返回结果格式为Json字符串
          System.out.println(response.getResult());
        } catch (Exception e) {
          e.printStackTrace();
        }
    }
}
 

是要查归属地,筛选出归属本地的用户吧?API示例都有了,遍历号码,对每一个号码填入mobile后面那个""处,调用API可以得到结果。

首先,可以使用一个循环遍历号码的4-7位(共1万个数据),并在每次迭代时设置查询的手机号。接下来,可以在循环体内构建一个号码归属地查询的API请求,并将查询的手机号作为查询参数添加到请求中。

在API响应中,可以解析响应的JSON字符串以获取号码归属地信息。然后,可以使用号码归属地信息作为筛选条件,将符合条件的号码存储到一个列表或数组中。

以下是一个可能的实现(需要在代码中填入自己的Access Key和Secret Key):


import com.baidubce.http.ApiExplorerClient;
import com.baidubce.http.AppSigner;
import com.baidubce.http.HttpMethodName;
import com.baidubce.model.ApiExplorerRequest;
import com.baidubce.model.ApiExplorerResponse;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class CrimeSuspectFinder {
    public static void main(String[] args) {
        String path = "http://gsd.api.bdymkt.com/sms";
        ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, path);
        request.setCredentials("YOUR_ACCESS_KEY", "YOUR_SECRET_KEY");

        request.addHeaderParameter("Content-Type", "application/json;charset=UTF-8");

        // 构建一个列表用于存储符合条件的号码
        List<String> matchingNumbers = new ArrayList<>();

        // 遍历号码的4-7位
        for (int i = 0; i < 10000; i++) {
            // 构建查询的手机号
            String phoneNumber = "135" + String.format("%04d", i) + "15";
            request.addQueryParameter("mobile", phoneNumber);

            ApiExplorerClient client = new ApiExplorerClient(new AppSigner());

            try {
                ApiExplorerResponse response = client.sendRequest(request);
                // 解析JSON字符串获取号码归属地信息
                JSONObject resultJson = new JSONObject(response.getResult());
                String province = resultJson.getString("province");
                String city = resultJson.getString("city");

                // 如果号码归属地符合筛选条件,则将号码添加到列表中
                if (province.equals("北京市") && city.equals("北京市")) {
                    matchingNumbers.add(phoneNumber);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            // 在每次迭代后,清除查询参数,以便下一次迭代使用
            request.removeQueryParameter("mobile");
        }

        // 输出符合条件的号码
        System.out.println("Matching phone numbers:");
        for (String number : matchingNumbers) {
            System.out.println(number);
        }
    }
}

在上述代码中,我们假设筛选条件为号码归属地为北京市。可以根据需要修改筛选条件。在循环中,我们使用String.format()方法将号码的4-7位填充为4位数字,然后将其添加到查询参数中。在每次迭代后,我们需要清除查询参数,以便下一次迭代使用。

在循环体内,我们使用JSONObject类解析API响应的JSON字符串,并从中获取号码归属地信息。如果号码归属地符合筛选条件,则将号码添加到列表中。最后,我们输出符合条件的号码。

使用API进行大量请求可能会受到访问频率的限制。如果API返回错误或超时,可以考虑增加请求之间的延迟时间,或者缩小查询的号码范围。

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^