接口地址: https://cx.shouji.360.cn/phonearea.php?number=手机号
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args0) throws IOException {
FileWriter fileWriter = new FileWriter("d://test1.txt");
String httpUrl = "https://cx.shouji.360.cn/phonearea.php";
String httpArg = "";
for (int i = 7000; i < 8000; i++) {
if (i >= 1000) {
httpArg = "number=188" + String.valueOf(i) + "4257";
System.out.println(httpArg);
} else if (i >= 100) {
httpArg = "number=1880" + String.valueOf(i) + "4257";
} else if (i >= 10) {
httpArg = "number=18800" + String.valueOf(i) + "4257";
} else {
httpArg = "number=188000" + String.valueOf(i) + "4257";
}
String jsonResult = request(httpUrl, httpArg);
String res = unicodeDecode(jsonResult);
if (res.contains("长沙")) {
System.out.println("号码:" + httpArg + "\t");
fileWriter.write(httpArg + "\n\t");
System.out.println("结果:" + res);
}
}
fileWriter.flush();
fileWriter.close();
}
/**
* @param
* :请求接口
* @param httpArg
* :参数
* @return 返回结果
*/
public static String request(String httpUrl, String httpArg) {
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
httpUrl = httpUrl + "?" + httpArg;
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
// 填入apikey到HTTP header
//connection.setRequestProperty("apikey", "0574246ccaa94772abaf4f8229414249");
connection.connect();
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
result = sbf.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* @Title: unicodeDecode
* @Description: unicode解码
* @param string
* @return
*/
public static String unicodeDecode(String string) {
Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
Matcher matcher = pattern.matcher(string);
char ch;
while (matcher.find()) {
ch = (char) Integer.parseInt(matcher.group(2), 16);
string = string.replace(matcher.group(1), ch + "");
}
return string;
}
}