自己实现的抓取网络信息

package util;

import java.io.IOException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.UUID; import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.cookie.CookiePathComparator;
import org.apache.http.cookie.CookieSpec;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpMethod; import BS; public class GetBsUtil { public static void main(String[] args) {
new GetBsUtil().getBsUtilByHttpClient("460","00","34860", "62041");
} public BS getBsUtilByHttpClient(String mcc , String mnc ,String lac,String cid){
CloseableHttpClient httpClient = HttpClients.createDefault();
String uri = "。。。。。。";
HttpGet httpGet = new HttpGet(uri);
CloseableHttpResponse httpResponse = null;
BS bs = new BS();
try {
httpGet.addHeader("Host", "。。。");
httpGet.addHeader("Accept", "*/*");
httpGet.addHeader("Accept-Encoding", "gzip,deflate,sdch");
httpGet.addHeader("Accept-Language", "zh-CN,zh;q=0.8");
httpGet.addHeader("Proxy-Connection", "keep-alive");
httpGet.addHeader("Referer", "。。。");
httpGet.addHeader("User-Agent", "。。。");
httpGet.addHeader("Cookie","。。。");
httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
if(null != entity){
String jsonEntity = EntityUtils.toString(entity);
System.out.println(jsonEntity);
jsonEntity = jsonEntity.substring(jsonEntity.indexOf("{") + 1, jsonEntity.length() - 2 );
String statusCode = "";
if(jsonEntity.contains("status")){
statusCode = jsonEntity.substring(jsonEntity.indexOf(":") + 2, jsonEntity.indexOf(",") - 1);
jsonEntity = jsonEntity.substring(jsonEntity.indexOf("{") + 1 , jsonEntity.indexOf("}") );
if("200".equals(statusCode)){
Field[] fields = bs.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
fields[i].setAccessible(true);
try {
if("lat".equals(fields[i].getName()) || "lng".equals(fields[i].getName())){
fields[i].set(bs, new BigDecimal(getSubDate(fields[i].getName(), jsonEntity).toString()));
}else if("lac".equals(fields[i].getName()) || "cid".equals(fields[i].getName())
|| "radius".equals(fields[i].getName()) || "rid".equals(fields[i].getName())){
fields[i].set(bs, getSubDate(fields[i].getName(), jsonEntity) != "" ? Integer.parseInt(getSubDate(fields[i].getName(), jsonEntity).toString()) : 0);
}else if("rids".equals(fields[i].getName())){
fields[i].set(bs, Long.parseLong(getSubDate(fields[i].getName(), jsonEntity).toString()));
}else if(!"createTime".equals(fields[i].getName())){
fields[i].set(bs, getSubDate(fields[i].getName(), jsonEntity));
}
} catch (Exception e) {
e.printStackTrace();
}
}
bs.setLac(Integer.parseInt(lac));
bs.setCid(Integer.parseInt(cid));
bs.setMcc(mcc);
bs.setMnc(mnc);
bs.setCreateTime(new Timestamp(new Date().getTime()));
bs.setId(UUID.randomUUID().toString());
System.out.println("bs:"+bs);
}
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(httpResponse != null) {
try {
EntityUtils.consume(httpResponse.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bs;
} private static Object getSubDate(String attrName , String jsonOld){
if(jsonOld.contains(attrName)){
if(jsonOld.indexOf(attrName) > 1){
jsonOld = jsonOld.substring(jsonOld.indexOf(attrName) - 1);
}
jsonOld = jsonOld.substring(jsonOld.indexOf(attrName) + attrName.length() + 2, jsonOld.indexOf(",") > -1 ? jsonOld.indexOf(",") : jsonOld.length());
return jsonOld.contains("\"") ? jsonOld.substring(1, jsonOld.length() - 1) : jsonOld;
}
return "";
} }
  

  

实体类:

package pojo;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp; public class BS implements Serializable { private String id;
private String mcc;
private String mnc;
private Integer lac;
private Integer cid;
private BigDecimal lat;
private BigDecimal lng;
private Integer radius;
private Integer rid;
private Long rids;
private String address;
private String roads;
private Timestamp createTime; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getMcc() {
return mcc;
} public void setMcc(String mcc) {
this.mcc = mcc;
} public String getMnc() {
return mnc;
} public void setMnc(String mnc) {
this.mnc = mnc;
} public Integer getLac() {
return lac;
} public void setLac(Integer lac) {
this.lac = lac;
} public Integer getCid() {
return cid;
} public void setCid(Integer cid) {
this.cid = cid;
} public BigDecimal getLat() {
return lat;
} public void setLat(BigDecimal lat) {
this.lat = lat;
} public BigDecimal getLng() {
return lng;
} public void setLng(BigDecimal lng) {
this.lng = lng;
} public Integer getRadius() {
return radius;
} public void setRadius(Integer radius) {
this.radius = radius;
} public Integer getRid() {
return rid;
} public void setRid(Integer rid) {
this.rid = rid;
} public Long getRids() {
return rids;
} public void setRids(Long rids) {
this.rids = rids;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public String getRoads() {
return roads;
} public void setRoads(String roads) {
this.roads = roads;
} public Timestamp getCreateTime() {
return createTime;
} public void setCreateTime(Timestamp createTime) {
this.createTime = createTime;
} @Override
public String toString() {
return "BS [id=" + id + ", mcc=" + mcc + ", mnc=" + mnc + ", lac="
+ lac + ", cid=" + cid + ", lat=" + lat + ", lng=" + lng
+ ", radius=" + radius + ", rid=" + rid + ", rids=" + rids
+ ", address=" + address + ", roads=" + roads + ", createTime="
+ createTime + "]";
} }

  

httpClient实现获取网络信息的更多相关文章

  1. 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化

    [源码下载] 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...

  2. 封装获取网络信息Linux—API类

    封装获取网络信息Linux—API类 封装好的库: #ifndef NETINFORMATION_H #define NETINFORMATION_H #include <netdb.h> ...

  3. 在C#中调用API获取网络信息和流量

    原文 在C#中调用API获取网络信息和流量 最近一项目中要求显示网络流量,而且必须使用C#. 事实上,调用 IpHlpApi.dll 的 GetIfTable API 可以轻易获得网络信息和网络流量. ...

  4. Android简易实战教程--第四十七话《使用OKhttp回调方式获取网络信息》

    在之前的小案例中写过一篇使用HttpUrlConnection获取网络数据的例子.在OKhttp盛行的时代,当然要学会怎么使用它,本篇就对其基本使用做一个介绍,然后再使用它的接口回调的方式获取相同的数 ...

  5. python 标准库获取网络信息

    c语言ioctl定义的常量 /usr/include/x86_64-linux-gnu/bits/ioctls.h /* Copyright (C) 1996-2015 Free Software F ...

  6. windows主机网络信息获取程序设计

    掌握windows系统获取网络信息的各种API函数的功能与调用方法,掌握设计程序显示获取到相关网络信息的方法,掌握网络字节数据与主机字节数据之间的转换.掌握这些API函数调用的错误处理方法. 利用本地 ...

  7. Perl获取主机名、用户、组、网络信息

    获取主机名.用户.组.网络信息相关函数 首先是获取主机名的方式,Perl提供了Sys::Hostname模块,可以查询当前的主机名: use Sys::Hostname; print hostname ...

  8. java使用siger 获取服务器硬件信息(CPU 内存 网络 io等)

    通过使用第三方开源jar包sigar.jar我们可以获得本地的信息 1.下载sigar.jar sigar官方主页 sigar-1.6.4.zip 2.按照主页上的说明解压包后将相应的文件copy到j ...

  9. 8021x 获取IP信息失败,请检查锐捷认证客户端当前配置是否符合所在网络的要求,检查完毕后尝试重新认证

    早上一起床,登陆锐捷客户端上网,谁知道错问题了.不能联网了,锐捷登陆成功,但是一会儿就提示失败,获取IP信息失败了.下面我描述一下问题原因: 锐捷登陆后有认证提示,和往常正常情况一样的,不过有个小感叹 ...

随机推荐

  1. throw 与 throws的应用

    throws---------->把异常交给调用处. 可以结合throw来同时使用. throws 用在方法声明处,表示本方法不处理异常.可以结合throw使用 throw 表示在方法中手工抛出 ...

  2. List中toArray()的使用方法

    当我们需要把一个链表中的元素放入数组时,jdk给我们提供了一种方法,也即运用toArray(),方法的使用如下: public class Test { public static void main ...

  3. 开启CURL扩展,让服务器支持PHP curl函数(远程采集)

    关于开启Curl的方法模板天下小编在此给大家简单说一下 curl().file_get_contents().snoopy.class.php这三个远程页面抓取或采集中用到的工具,默迹还是侵向于用sn ...

  4. android 为组件添加contextMenu上下文菜单

    package com.example.fragmentNavigation2.fragment; import android.os.Bundle; import android.support.v ...

  5. 在eclipse中将项目发布到tomcat的root目录

    (1)设置项目上下文,右击项目-properties   >Web Page Edit

  6. *[topcoder]AstronomicalRecords

    http://community.topcoder.com/stat?c=problem_statement&pm=12804&rd=15706 首先A和B的长度都不一定一样,里面的元 ...

  7. Yii CDbCriteria

    Yii的Active Recorder包装了很多. 特别是把SQL中 把where,order,limit,IN/not IN,like等常用短句都包含进CDbCriteria这个类中去,这样整个代码 ...

  8. JNI编程(一) —— 编写一个最简单的JNI程序

    来自:http://chnic.iteye.com/blog/198745 忙了好一段时间,总算得了几天的空闲.貌似很久没更新blog了,实在罪过.其实之前一直想把JNI的相关东西整理一下的,就从今天 ...

  9. Moduli number system

    A number system with moduli is defined by a vector of k moduli, [m1,m2, ···,mk]. The moduli must be p ...

  10. Hibernate一级缓存、二级缓存

    缓存就是把以前从数据库中查询出来和使用过的对象保存在内存中,准确说就是一个数据结构中,这个数据结构通常是或类似HashMap,当以后要使用某个对象时,先查询缓存中是否有这个对象,如果有则使用缓存中的对 ...