package unfinalized.http.invoker;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
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 org.json.JSONObject;
import org.junit.Test;
public class HttpInvokerTests {
@Test
public void contextLoads() throws IOException {
CloseableHttpClient client = HttpClients.createDefault();
Map<String, String> cache = new HashMap<>();
try {
long start = System.currentTimeMillis();
HttpGet request = new HttpGet("http://wapsc.189.cn/numberOption/queryNumIndex/1?cityCode=" + "0128" + "&rowNum=1000");
while (true) {
CloseableHttpResponse response = client.execute(request);
//success
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//读取服务器返回过来的json字符串数据
HttpEntity entity = response.getEntity();
String str = EntityUtils.toString(entity, "utf-8");
//把json字符串转换成json对象
JSONObject json = new JSONObject(str);
JSONObject object = json.getJSONObject("jsonArray");
Iterator keys = object.keys();
while (keys.hasNext()) {
Object next = keys.next();
cache.put(next.toString(), object.getJSONObject(next.toString()).toString());
}
}
// if (System.currentTimeMillis() - start >= 2 * 60 * 1000 || cache.size() > 10000) {
if (cache.size() > 100) {
break;
}
}
//persist
} catch (Exception e) {
e.printStackTrace();
}
FileWriter fw = new FileWriter("test.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
// Iterator iterator = cache.keySet().iterator();
Set<String> keySet = cache.keySet();
for (String key : keySet) {
try {
String strValue = cache.get(key);
JSONObject json = new JSONObject(strValue);
// bw.append(key).append(":").append(cache.get(key)).append("\r\n");
bw.append(json.getString("accNbr1")).append(" ");
// if (cache.size()%10==0) {
// bw.append("\r\n");
// }
} catch (Exception e) {
e.printStackTrace();
}
};
bw.flush();
bw.close();
fw.close();
// });
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
httpClient 调用远程接口 获取数据到本地文件夹
共17个文件
properties:4个
prefs:3个
java:2个
需积分: 50 26 下载量 48 浏览量
2018-04-24
15:20:26
上传
评论
收藏 50KB RAR 举报
温馨提示
本例子是通过httpClient 不断的去电信的网站查询可选手机新号 , 每次查出来的数据保存到map , 到一定数量以后 , 保存在本地文件中 , 测试类可以直接运行.
资源推荐
资源详情
资源评论
收起资源包目录
http-invoker.rar (17个子文件)
http-invoker
.gitignore 249B
.project 772B
.mvn
wrapper
maven-wrapper.jar 46KB
maven-wrapper.properties 110B
pom.xml 2KB
target
generated-test-sources
test-annotations
test-classes
unfinalized
http
invoker
HttpInvokerTests.class 4KB
generated-sources
annotations
classes
META-INF
maven
unfinalized.http.invoker
http-invoker
pom.xml 2KB
pom.properties 263B
MANIFEST.MF 365B
unfinalized
http
invoker
application.properties 0B
.settings
org.eclipse.core.resources.prefs 155B
org.eclipse.jdt.core.prefs 430B
org.eclipse.m2e.core.prefs 90B
src
test
java
unfinalized
http
invoker
HttpInvokerTests.java 3KB
main
resources
static
templates
application.properties 0B
java
unfinalized
http
invoker
HttpInvoker.java 318B
.classpath 1KB
共 17 条
- 1
资源评论
机智的大黄鱼
- 粉丝: 11
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功