码迷,mamicode.com
首页 > Web开发 > 详细

httpClient实现获取网络信息

时间:2015-08-05 10:14:45      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

 

 

自己实现的抓取网络信息

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 com.renmaiweb.lbs.pojo.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实现获取网络信息

标签:

原文地址:http://www.cnblogs.com/a757956132/p/4703784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!