大家好,又见面了,我是你们的朋友全栈君。
我也不知道咋写的,测试过了,反正能用就行;
<!--httpclient依赖包-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<!--日志包-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
package com.example.needs.util;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.*;
public class TextUtil {
public static void main(String[] args) throws IOException {
FileWriter fileWriter = new FileWriter("D:/Lianxi/zidian.txt");
String httpUrl = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=";
String zd = "";
for (int i = 0; i < 100; i++) {
if (i >= 10) {
zd = "1785" + String.valueOf(i) + "08019";
} else {
zd = "178500" + String.valueOf(i) + "08019";
}
String js = doGet(httpUrl,zd);
if (js.contains("山东")){
fileWriter.write(zd+"\r\n");
}
}
fileWriter.flush();
fileWriter.close();
}
/** * 发送HttpGet请求 * @param url * @return */
public static String doGet(String url,String zd) {
//1.获得一个httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
//2.生成一个get请求
HttpGet httpget = new HttpGet(url+zd);
CloseableHttpResponse response = null;
try {
//3.执行get请求并返回结果
response = httpclient.execute(httpget);
} catch (IOException e1) {
e1.printStackTrace();
}
String result = null;
try {
//4.处理结果,这里将结果返回为字符串
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
}
} catch (ParseException | IOException e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/163175.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...