赞
踩
我也不知道咋写的,测试过了,反正能用就行;
<!--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; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。