Java编写ip代理池(一)

各位盟友,第一次在github上发布源码,不足之处还往多多指教。

     这是给朋友爬虫使用的ip代理池,保证ip的有效性和可靠性,使用java实现。

      话不多说开始上干货

     本次采用的是apache开源对象池PoolableObjectFactory管理所有ip

    首先引项目所需依赖

        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    4.0.0

   

        org.springframework.boot

        spring-boot-starter-parent

        2.1.3.RELEASE

       

   

    com.shimmer

    iphttps

    0.0.1-SNAPSHOT

    iphttps

    Demo project for Spring Boot

   

        1.8

   

   

       

            org.springframework.boot

            spring-boot-starter-web

       

       

            org.springframework.boot

            spring-boot-starter-test

            test

       

       

            org.apache.commons

            commons-lang3

            3.7

       

       

            commons-codec

            commons-codec

       

       

            commons-httpclient

            commons-httpclient

            3.1

       

       

            org.apache.poi

            poi

            3.9

       

       

            org.apache.poi

            poi-ooxml

            3.9

       

       

            com.alibaba

            fastjson

            1.2.47

       

       

            commons-pool

            commons-pool

            1.6

       

       

            org.apache.httpcomponents

            httpclient

            4.5.2

       

       

            org.springframework.boot

            spring-boot-starter-test

            test

       

       

            org.junit.jupiter

            junit-jupiter-api

            RELEASE

       

       

            org.springframework.boot

            spring-boot-test

            2.1.3.RELEASE

       

       

            junit

            junit

       

       

            org.springframework

            spring-test

            5.1.5.RELEASE

       

   

   

       

           

                org.springframework.boot

                spring-boot-maven-plugin

           

       

   

核心配置文件application.yml

# PROXY代理配置

ip-proxy:

  # 最大活动数

  maxActive: 200

  # 最大空闲数

  maxIdle: 20

  # 最大等待时间

  maxWait: 100

  #返回是否校验

  testOnReturn: false

  #借出是否校验

  testOnBorrow: true

  #是否后进先出

  lifo: false

  #提前多少分钟销毁对象

  testTime: 3

  #设定间隔每过多少毫秒进行一次后台对象清理的行动。如果这个值不是正数,则实际上不会进行后台对象清理。

  timeBetweenEvictionRunsMillis: -1000

  #设定在进行后台对象清理时,是否还对没有过期的池内对象进行有效性检查。不能通过有效性检查的对象也将被回收。

  testWhileIdle: true

  #设定在进行后台对象清理时,每次检查几个对象。如果这个值不是正数,则每次检查的对象数是检查时池内对象的总数乘以这个值的负倒数再向上取整的结果――也就是说,

  #如果这个值是-2(-3、-4、-5……)的话,那么每次大约检查当时池内对象总数的1/2(1/3、1/4、1/5……)左右。

  numTestsPerEvictionRun: 3

  # 获取ip 方式 0 为免费 1 为付费 2 为免费和付费同时进行

  iPType: 2

  # IP代理请求路径 某某第三方ip代理

  httpPath: "http://"

  # IP代理请求参数

  httpProxy:

  #所有的参数和对应的值 如下:

    num: "1"

    type: "2"

    pro: ""

    city: ""

    yys: "0"

    port: "1"

    pack: "1111"

    amount: "1"

    ts: "1"

    ys: "1"

    cs: "1"

    lb: "1"

    sb: "0"

    pb: "4"

    mr: "1"

    regions: ""

获取配置类 

package com.sunshine.http_proxy.utils;

import java.util.HashMap;

import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.context.annotation.Configuration;

/**

* @author xaingzi

* 配置类

* 从配置文件中读取数据映射到map

* 注意:必须实现set方法

*/

@Configuration

@ConfigurationProperties(prefix = "ip-proxy")

public class HttpProxyConfig {

  private int maxActive;

  private int maxIdle;

  private int maxWait;

  private int testTime;

  private int numTestsPerEvictionRun;

  private int iPType;

  private boolean testOnReturn;

  private boolean testOnBorrow;

  private boolean lifo;

  private String httpPath;

  private boolean testWhileIdle;

  private long timeBetweenEvictionRunsMillis;

  private Map httpProxy = new HashMap<>();


  public int getMaxActive() {

      return maxActive;

  }

  public void setMaxActive(int maxActive) {

      this.maxActive = maxActive;

  }

  public int getMaxIdle() {

      return maxIdle;

  }

  public void setMaxIdle(int maxIdle) {

      this.maxIdle = maxIdle;

  }

  public int getMaxWait() {

      return maxWait;

  }

  public void setMaxWait(int maxWait) {

      this.maxWait = maxWait;

  }

  public boolean isTestOnReturn() {

      return testOnReturn;

  }

  public void setTestOnReturn(boolean testOnReturn) {

      this.testOnReturn = testOnReturn;

  }

  public boolean isTestOnBorrow() {

      return testOnBorrow;

  }

  public void setTestOnBorrow(boolean testOnBorrow) {

      this.testOnBorrow = testOnBorrow;

  }

  public boolean isLifo() {

      return lifo;

  }

  public void setLifo(boolean lifo) {

      this.lifo = lifo;

  }

  public String getHttpPath() {

      return httpPath;

  }

  public void setHttpPath(String httpPath) {

      this.httpPath = httpPath;

  }

  public Map getHttpProxy() {

      return httpProxy;

  }

  public void setHttpProxy(Map httpProxy) {

      this.httpProxy = httpProxy;

  }

  public int getTestTime() {

      return testTime;

  }

  public void setTestTime(int testTime) {

      this.testTime = testTime;

  }

  public boolean isTestWhileIdle() {

      return testWhileIdle;

  }

  public void setTestWhileIdle(boolean testWhileIdle) {

      this.testWhileIdle = testWhileIdle;

  }

  public long getTimeBetweenEvictionRunsMillis() {

      return timeBetweenEvictionRunsMillis;

  }

  public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {

      this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;

  }

    public int getNumTestsPerEvictionRun() {

        return numTestsPerEvictionRun;

    }

    public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {

        this.numTestsPerEvictionRun = numTestsPerEvictionRun;

    }

    public int getiPType() {

        return iPType;

    }

    public void setiPType(int iPType) {

        this.iPType = iPType;

    }

}


ip对象存储基类

package com.sunshine.http_proxy.pool;

/**

* 代理对象

*

* @author xaingzi

*/

import java.io.Serializable;

public class ProxyHost implements Serializable {

    /**

    * 序列化

    */

    private static final long serialVersionUID = 1L;

    private String ip;

    private Integer port;

    private String creaTime;

    private String expireTime;

    private String city;

    private String isp;

    public String getIp() {

        return ip;

    }

    public void setIp(String ip) {

        this.ip = ip;

    }

    public Integer getPort() {

        return port;

    }

    public void setPort(Integer port) {

        this.port = port;

    }

    public String getCreaTime() {

        return creaTime;

    }

    public void setCreaTime(String creaTime) {

        this.creaTime = creaTime;

    }

    public String getExpireTime() {

        return expireTime;

    }

    public void setExpireTime(String expireTime) {

        this.expireTime = expireTime;

    }

    public String getCity() {

        return city;

    }

    public void setCity(String city) {

        this.city = city;

    }

    public String getIsp() {

        return isp;

    }

    public void setIsp(String isp) {

        this.isp = isp;

    }

    @Override

    public String toString() {

        return "ip:" + ip + " 端口:" + port + "  到期时间:" + expireTime + " 创建时间:" + creaTime + " 城市:" + city + " isp:" + isp;

    }

}

http请求获取第三方代理ip和扫描免费代理ip 

package com.sunshine.http_proxy;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.InetSocketAddress;

import java.net.MalformedURLException;

import java.net.Proxy;

import java.net.URL;

import java.net.URLConnection;

import java.text.SimpleDateFormat;

import java.util.*;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

import com.sunshine.http_proxy.utils.DateUtil;

import com.sunshine.http_proxy.utils.HttpClientUtil;

import com.sunshine.http_proxy.utils.HttpProxyConfig;

import org.apache.commons.httpclient.HttpClient;

import org.apache.commons.lang3.StringUtils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;

/**

*

* @author xaingzi

*

*/

@SuppressWarnings({"unchecked", "rawtypes"})

public class HttpProxyClientLocat implements Runnable {

    public static Logger log = LoggerFactory.getLogger(HttpProxyClientLocat.class);

    private static final String BAIDU_PATH = "http://youxingyang.com";

    private static Map hashMap = new Hashtable();

    private static JSONObject json = new JSONObject();

    private static int[] ports = new int[]{80, 443, 8883, 4271, 8886, 8080, 8888, 8887, 8889, 8081};

    private static String expireTime, city, isp;

    private static int i = 0;

    /**

    * @param path

    * @param map

    * @return

    */

    public static JSONObject getHttpClientProxyIp(String path, Map map) {

        List list = new ArrayList>();

        JSONObject json = new JSONObject();

        do {

            String result = HttpClientUtil.httpGet(path, map);

            log.info("获取ip信息:" + result);

            List parseArray = JSON.parseArray(JSON.parseObject(result).getString("data"), String.class);

            for (String str : parseArray) {

                String ip = JSON.parseObject(str).getString("ip");

                String port = JSON.parseObject(str).getString("http_port_secured");

                // String id =

                // JSON.parseObject(result).getJSONArray("data").getJSONObject(0).getString("id");

                Map map1 = new HashMap();

                map1.put(ip, Integer.valueOf(port));

                checkProxyIp(map1, BAIDU_PATH);

            }

        } while ((i < 3) && (hashMap.size() < 1));

        list.add(hashMap);

        json.put("status", 0);

        json.put("data", list);

        return json;

    }

    /**

    * @param httpProxyConfig

    * @return

    */

    public static JSONObject getHttpClientProxyIp(HttpProxyConfig httpProxyConfig) {

        hashMap.clear();

        JSONObject json = new JSONObject();

        if (httpProxyConfig.getiPType() == 0) {

            try {

                json = getHttpChilentProxyApi();

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        } else if (httpProxyConfig.getiPType() == 1) {

            json = getHttpClient(httpProxyConfig);

        } else if (httpProxyConfig.getiPType() == 2) {

            if (Math.random() > 0.3) {

                json = getHttpClient(httpProxyConfig);

            } else {

                try {

                    json = getHttpChilentProxyApi();

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

            }

        }

        return json;

    }

    private static JSONObject getHttpClient(HttpProxyConfig httpProxyConfig) {

        List list = new ArrayList>();

        do {

            log.info("发送Get请求:" + httpProxyConfig.getHttpPath());

            String result = HttpClientUtil.httpGet(httpProxyConfig.getHttpPath(), httpProxyConfig.getHttpProxy());

            log.info("获取ip信息:" + result);

            List parseArray = JSON.parseArray(JSON.parseObject(result).getString("data"), String.class);

            for (String str : parseArray) {

                String ip = JSON.parseObject(str).getString("ip");

                String port = JSON.parseObject(str).getString("port");

                expireTime = JSON.parseObject(str).getString("expire_time");

                isp = city = JSON.parseObject(str).getString("isp");

                city = JSON.parseObject(str).getString("city");

                // String id =

                // JSON.parseObject(result).getJSONArray("data").getJSONObject(0).getString("id");

                Map map1 = new HashMap();

                map1.put(ip, Integer.valueOf(port));

                checkProxyIp(map1, BAIDU_PATH);

            }

        } while ((i < 3) && (hashMap.size() < 1));

        list.add(hashMap);

        json.put("status", 0);

        json.put("data", list);

        return json;

    }

    /**

    * 单个代理IP有效检测

    *

    * @param ip

    * @param port

    */

    public void createIPAddress(String ip, int port) {

        URL url = null;

        try {

            url = new URL("http://www.baidu.com");

        } catch (MalformedURLException e) {

            log.info("url invalidate");

        }

        InetSocketAddress addr = null;

        addr = new InetSocketAddress(ip, port);

        Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http proxy

        InputStream in = null;

        try {

            URLConnection conn = url.openConnection(proxy);

            conn.setConnectTimeout(1000);

            in = conn.getInputStream();

        } catch (Exception e) {

            log.info(Thread.currentThread().getName() + "  ip " + ip + " : " + port + "  is not aviable");// 异常IP

        }

        String s = convertStreamToString(in);

        //log.info(s);

        // log.info(s);http://ip.taobao.com/service/getIpInfo.php?ip=210.32.158.14

            synchronized (this){

                if (s.indexOf("html") > 0) {// 有效IP

                        log.info(Thread.currentThread().getName() + "  " + ip + ":" + port + " is ok");

                        hashMap.put("ip", ip);

                        hashMap.put("port", port + "");

                        String s1 = HttpClientUtil.getResult("http://ip.taobao.com/service/getIpInfo.php", "ip=" + ip, "utf-8");

                        //String[] split = JSON.parseObject(s1).getJSONArray("data").getJSONObject(0).getString("location")

                        //.split(" ");

                        JSONObject data = JSON.parseObject(JSON.parseObject(s1).getString("data"));

                        hashMap.put("ip", ip);

                        hashMap.put("port", port + "");

                        hashMap.put("creaTime", (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));

                        hashMap.put("expireTime", DateUtil.format(DateUtil.addMinute(new Date(), 30), DateUtil.TIME_PATTERN_DEFAULT));

                        if (StringUtils.equals(data.getString("region"), data.getString("city"))) {

                            hashMap.put("city", data.getString("region") + "市" + data.getString("city") + "市");

                        } else {

                            hashMap.put("city", data.getString("region") + "省" + data.getString("city") + "市");

                        }

                        hashMap.put("isp", data.getString("isp"));

                }

            }

    }

    public static JSONObject getHttpChilentProxyApi() throws InterruptedException {

        Vector vector = new Vector();

        log.info("获取免费ip");

        Vector vec = new Vector();

        HttpProxyClientLocat httptest2 = new HttpProxyClientLocat();

        int threadNumber = 320;

        for (int i = 0; i < threadNumber; i++) {

            Thread thread = new Thread(httptest2);

            thread.start();

            vec.add(thread);

        }

        for (Thread thread : vec) {

            thread.join();

        }

        vector.add(hashMap);

        json.put("status", 0);

        json.put("data", vector);

        log.info(json.toString());

        return json;

    }

    @Override

    public void run() {

        while (hashMap.size() < 1) {

            String randomIp = getRandomIp();

            log.info(randomIp);

            for (int port : ports) {

                createIPAddress(randomIp, port);

            }

        }

    }

    public static String convertStreamToString(InputStream is) {

        if (is == null)

            return "";

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        StringBuilder sb = new StringBuilder();

        String line = null;

        try {

            while ((line = reader.readLine()) != null) {

                sb.append(line + "/n");

            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                is.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

        return sb.toString();

    }

    /**

    * 批量代理IP有效检测

    *

    * @param proxyIpMap

    * @param reqUrl

    */

    public static void checkProxyIp(Map proxyIpMap, String reqUrl) {

        log.info("检测ip是否有效");

        for (String proxyHost : proxyIpMap.keySet()) {

            Integer proxyPort = proxyIpMap.get(proxyHost);

            int statusCode = 0;

            try {

                HttpClient httpClient = new HttpClient();

                httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);

                // 连接超时时间(默认10秒 10000ms) 单位毫秒(ms)

                int connectionTimeout = 10000;

                // 读取数据超时时间(默认30秒 30000ms) 单位毫秒(ms)

                int soTimeout = 30000;

                httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);

                httpClient.getHttpConnectionManager().getParams().setSoTimeout(soTimeout);

                String httpGet = HttpClientUtil.httpGet(reqUrl, new HashMap<>());

                log.info(httpGet);

                statusCode = httpGet.indexOf("html");

            } catch (Exception e) {

                i++;

                log.error("ip " + proxyHost + " is not aviable");

            }

            Lock l = new ReentrantLock();

            l.lock();

            try {

                if (statusCode > 0) {

                    log.info(Thread.currentThread().getName() + "  " + proxyHost + ":" + proxyPort + " is ok");

                    System.out.format("%s:%s-->%s/n", proxyHost, proxyPort, statusCode);

                    hashMap.put("creaTime", (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));

                    hashMap.put("expireTime", expireTime);

                    hashMap.put("city", city);

                    hashMap.put("ip", proxyHost);

                    hashMap.put("port", proxyPort + "");

                }

            } finally {

                l.unlock();

            }

        }

    }

    public static JSONObject connectHttpChilentStatus(String http) {

        List list = new ArrayList>();

        log.info("开始校验" + http);

        JSONObject json = new JSONObject();

        if (null != http && "" != http) {

            String[] ips = http.split(",");

            for (String ip : ips) {

                if (null != ip && "" != ip) {

                    Map map = new HashMap<>();

                    String[] str = ip.split(":");

                    int statusCode = 0;

                    map.put("ip", str[0]);

                    map.put("port", str[1]);

                    try {

                        HttpClient httpClient = new HttpClient();

                        httpClient.getHostConfiguration().setProxy(str[0], Integer.valueOf(str[1]));

                        // 连接超时时间(默认10秒 10000ms) 单位毫秒(ms)

                        int connectionTimeout = 10000;

                        // 读取数据超时时间(默认30秒 30000ms) 单位毫秒(ms)

                        int soTimeout = 30000;

                        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);

                        httpClient.getHttpConnectionManager().getParams().setSoTimeout(soTimeout);

                        String httpGet = HttpClientUtil.httpGet(BAIDU_PATH, new HashMap<>());

                        log.info(httpGet);

                        statusCode = httpGet.indexOf("html");

                    } catch (Exception e) {

                        map.put("status", 500);

                        map.put("msg", "连接超时");

                    }

                    if (statusCode > 0) {

                        map.put("status", 0);

                        map.put("msg", "连接成功");

                    }

                    list.add(map);

                }

            }

        }

        json.put("status", 0);

        json.put("data", list);

        return json;

    }

    public static String getRandomIp() {

        // ip范围

        int[][] range = {{607649792, 608174079}, // 36.56.0.0-36.63.255.255

                {1038614528, 1039007743}, // 61.232.0.0-61.237.255.255

                {1783627776, 1784676351}, // 106.80.0.0-106.95.255.255

                {2035023872, 2035154943}, // 121.76.0.0-121.77.255.255

                {2078801920, 2079064063}, // 123.232.0.0-123.235.255.255

                {-1950089216, -1948778497}, // 139.196.0.0-139.215.255.255

                {-1425539072, -1425014785}, // 171.8.0.0-171.15.255.255

                {-1236271104, -1235419137}, // 182.80.0.0-182.92.255.255

                {-770113536, -768606209}, // 210.25.0.0-210.47.255.255

                {-569376768, -564133889}, // 222.16.0.0-222.95.255.255

        };

        Random rdint = new Random();

        int index = rdint.nextInt(10);

        String ip = num2ip(range[index][0] + new Random().nextInt(range[index][1] - range[index][0]));

        return ip;

    }

    public static String num2ip(int ip) {

        int[] b = new int[4];

        String x = "";

        b[0] = (int) ((ip >> 24) & 0xff);

        b[1] = (int) ((ip >> 16) & 0xff);

        b[2] = (int) ((ip >> 8) & 0xff);

        b[3] = (int) (ip & 0xff);

        x = Integer.toString(b[0]) + "." + Integer.toString(b[1]) + "." + Integer.toString(b[2]) + "."

                + Integer.toString(b[3]);

        return x;

    }

}

因为用到了时间转换和 httpClient请求  所以我也把这两个工具类贴出来 方便使用

httpClientUtil

package com.sunshine.http_proxy.utils;

import java.io.*;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import org.apache.http.HttpEntity;

import org.apache.http.NameValuePair;

import org.apache.http.client.config.CookieSpecs;

import org.apache.http.client.config.RequestConfig;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

public class HttpClientUtil {

    public static String httpGet(String url, Map params) {

        CloseableHttpClient httpClient = HttpClients.createDefault();

        Iterator keysIte = params.keySet().iterator();

        int index = 0;

        while (keysIte.hasNext()) {

            String key = keysIte.next();

            if (index == 0) {

                url = url + "?" + key + "=" + params.get(key);

            } else {

                url = url + "&" + key + "=" + params.get(key);

            }

            index++;

        }

        HttpGet httpget = new HttpGet(url);

        CloseableHttpResponse response = null;

        StringBuilder result = new StringBuilder();

        // 设置请求和传输超时时间5s,设置cookie策略

        RequestConfig requestconfig = RequestConfig.custom()

                .setSocketTimeout(5000).setConnectTimeout(5000)

                .setCookieSpec(CookieSpecs.BEST_MATCH).build();

        httpget.setConfig(requestconfig);

        try {

            response = httpClient.execute(httpget);

            HttpEntity entity = response != null ? response.getEntity() : null;

            if (entity != null) {

                InputStream in = entity.getContent();

                BufferedReader br = new BufferedReader(new InputStreamReader(

                        in, "UTF-8"));

                String line;

                while ((line = br.readLine()) != null) {

                    result.append(line);

                    result.append("\n");

                }

                in.close();

                br.close();

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            try {

                if (response != null) response.close();

                httpClient.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

        return result.toString();

    }

    public static String httpPost(String url, String body) {

        CloseableHttpClient httpClient = HttpClients.createDefault();

        HttpPost httpPost = new HttpPost(url);

        CloseableHttpResponse response = null;

        String result = null;

        try {

            StringEntity stringEntity = new StringEntity(body, "UTF-8");

            httpPost.setEntity(stringEntity);

            response = httpClient.execute(httpPost);

            HttpEntity entity = response != null ? response.getEntity() : null;

            if (entity != null) {

                result = EntityUtils.toString(entity, "UTF-8");

            }

            EntityUtils.consume(entity);

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            try {

                if (response != null) response.close();

                httpClient.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

        return result;

    }

    public static String httpPost(String url, Map params) {

        CloseableHttpClient httpClient = HttpClients.createDefault();

        HttpPost httpPost = new HttpPost(url);

        CloseableHttpResponse response = null;

        String result = null;

        try {

            List nvps = new ArrayList();

            for (String key : params.keySet()) {

                nvps.add(new BasicNameValuePair(key, params.get(key)));

            }

            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));

            response = httpClient.execute(httpPost);

            HttpEntity entity = response != null ? response.getEntity() : null;

            if (entity != null) {

                result = EntityUtils.toString(entity, "UTF-8");

            }

            EntityUtils.consume(entity);

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            try {

                if (response != null) response.close();

                httpClient.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

        return result;

    }

    public static String getBodyString(BufferedReader br) {

        String inputLine;

        String str = "";

        try {

            while ((inputLine = br.readLine()) != null) {

                str += inputLine;

            }

            br.close();

        } catch (IOException e) {

            System.out.println("IOException: " + e);

        }

        return str;

    }

    /**

    * 设置url以及键值对参数

    *

    * @param url

    * @param params

    * @return

    */

    public static String getUrlWithParams(String url, Map params) {

        Iterator keysIte = params.keySet().iterator();

        int index = 0;

        while (keysIte.hasNext()) {

            String key = keysIte.next();

            if (index == 0) {

                url = url + "?" + key + "=" + params.get(key);

            } else {

                url = url + "&" + key + "=" + params.get(key);

            }

            index++;

        }

        return url;

    }

    /**

    * 按照一定顺序设置url以及键值对参数

    *

    * @param url

    * @param params

    * @param orderList

    * @return

    */

    public static String getUrlWithParams(String url, Map params, List orderList) {

        int index = 0;

        for (String key : orderList) {

            if (index == 0) {

                url = url + "?" + key + "=" + params.get(key);

            } else {

                url = url + "&" + key + "=" + params.get(key);

            }

            index++;

        }

        return url;

    }

    /**

    * @param urlStr  请求的地址

    * @param content  请求的参数 格式为:name=xxx&pwd=xxx

    * @param encoding 服务器端请求编码。如GBK,UTF-8等

    * @return

    */

    public static String getResult(String urlStr, String content, String encoding) {

        URL url = null;

        HttpURLConnection connection = null;

        try {

            url = new URL(urlStr);

            connection = (HttpURLConnection) url.openConnection();// 新建连接实例

            connection.setConnectTimeout(2000);// 设置连接超时时间,单位毫秒

            connection.setReadTimeout(2000);// 设置读取数据超时时间,单位毫秒

            connection.setDoOutput(true);// 是否打开输出流 true|false

            connection.setDoInput(true);// 是否打开输入流true|false

            connection.setRequestMethod("GET");// 提交方法POST|GET

            connection.setUseCaches(false);// 是否缓存true|false

            connection.connect();// 打开连接端口

            DataOutputStream out = new DataOutputStream(connection

                    .getOutputStream());// 打开输出流往对端服务器写数据

            out.writeBytes(content);// 写数据,也就是提交你的表单 name=xxx&pwd=xxx

            out.flush();// 刷新

            out.close();// 关闭输出流

            BufferedReader reader = new BufferedReader(new InputStreamReader(

                    connection.getInputStream(), encoding));// 往对端写完数据对端服务器返回数据

            // ,以BufferedReader流来读取

            StringBuffer buffer = new StringBuffer();

            String line = "";

            while ((line = reader.readLine()) != null) {

                buffer.append(line);

            }

            reader.close();

            return buffer.toString();

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            if (connection != null) {

                connection.disconnect();// 关闭连接

            }

        }

        return null;

    }

}

DateUtil

/**

 * 时间操作工具

 *

* @author xaingzi

*

*/

package com.sunshine.http_proxy.utils;

import org.apache.commons.lang3.StringUtils;

import javax.xml.datatype.DatatypeConfigurationException;

import javax.xml.datatype.DatatypeFactory;

import javax.xml.datatype.XMLGregorianCalendar;

import java.sql.Timestamp;

import java.text.DateFormatSymbols;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.*;

/**

* 日期转换工具

*/

public class DateUtil {

  public static final String DATE_DIVISION = "-";

  /**

    * yyyy-MM-dd HH:mm:ss

    */

  public static final String TIME_PATTERN_DEFAULT = "yyyy-MM-dd HH:mm:ss";

  public static final String TIME_PATTERN_DEFAULT_NO_SS = "yyyy-MM-dd HH:mm";

  /**

    * yyyy-MM-dd

    */

  public static final String DATE_PATTERN_DEFAULT = "yyyy-MM-dd";

  /**

    * yyyyMMdd

    */

  public static final String DATA_PATTERN_YYYYMMDD = "yyyyMMdd";

  /**

    * HH:mm:ss

    */

  public static final String TIME_PATTERN_HHMMSS = "HH:mm:ss";

  /**

    * yyyy-MM-dd HH-mm-ss

    */

  public static final String TIME_PATTERN = "yyyy-MM-dd HH-mm-ss";

  /**

    * yyyyMMddHHmmss

    */

  public static final String TIME_PATTERN_DEFAULT_STR = "yyyyMMddHHmmss";

  public static final String TIME_PATTERN_SSSS_STR = "yyyyMMddHHmmssSSSS";

  public static final int SECOND = 1000;

  public static final int MINUTE = 60 * SECOND;

  public static final int HOUR = 60 * MINUTE;

  public static final long DAY = 24l * HOUR;

  /**

    * Return the current date

    *

    * @return - DATE

    */

  public static Date now() {

      Calendar cal = Calendar.getInstance();

      Date currDate = cal.getTime();

      return currDate;

  }

  public static Timestamp nowTimestamp() {

      Calendar cal = Calendar.getInstance();

      return new Timestamp(cal.getTimeInMillis());

  }

  /**

    * 给一个时间加上N分种或减去N分种后得到一个新的日期

    *

    * @param startDate

    *            需要增加的日期时间

    * @param addnos

    *            添加的分钟数,可以是正数也可以是负数

    * @return 操作后的日期

    */

  public static Date addMinute(Date startDate, int addnos) {

      if (startDate == null) {

        return null;

      }

      Calendar cc = Calendar.getInstance();

      cc.setTime(startDate);

      cc.add(Calendar.MINUTE, addnos);

      return cc.getTime();

  }

  /**

    * Return the current date string

    *

    * @return - 产生的日期字符串

    */

  public static String nowString() {

      Calendar cal = Calendar.getInstance();

      Date currDate = cal.getTime();

      return formatDate(currDate);

  }

  /**

    * Return the current date in the specified format

    *

    * @param strFormat

    * @return

    */

  public static String nowString(String pattern) {

      Calendar cal = Calendar.getInstance();

      Date currDate = cal.getTime();

      return format(currDate, pattern);

  }

  public static Timestamp parseTimestampDefault(String dateValue) {

      return new Timestamp(parse(dateValue, TIME_PATTERN_DEFAULT).getTime());

  }

  public static Timestamp parseTimestamp(String dateValue) {

      return new Timestamp(parse(dateValue, TIME_PATTERN_HHMMSS).getTime());

  }

  public static Timestamp parseTimestampYYMMDD(String dateValue) {

      return new Timestamp(parse(dateValue, DATE_PATTERN_DEFAULT).getTime());

  }


  public static Timestamp parseTimestamp(String dateValue,String partten) {

      return new Timestamp(parse(dateValue, partten).getTime());

  }

  public static Timestamp parseTimeNoStamp(String dateValue) {

      if(null==dateValue||dateValue.trim().length()==0) {

        dateValue=getNowDateString(); 

      }

      return new Timestamp(parse(dateValue, DATE_PATTERN_DEFAULT).getTime());

  }

  public static String getNowDateString() {

      return format(new Date(),DATE_PATTERN_DEFAULT);

  }

  /**

    * Parse a string and return a date value

    *

    * @param dateValue

    * @return

    * @throws Exception

    */

  public static Date parseDate(String dateValue) {

      return parse(dateValue, DATE_PATTERN_DEFAULT);

  }

  /**

    * Parse a strign and return a datetime value

    *

    * @param dateValue

    * @return

    */

  public static Date parseTime(String dateValue) {

      return parse(dateValue, TIME_PATTERN_DEFAULT);

  }

  /**

    * Parse a string and return the date value in the specified format

    *

    * @param strFormat

    * @param dateValue

    * @return

    * @throws ParseException

    * @throws Exception

    */

  public static Date parse(String dateValue, String pattern) {

      if (StringUtils.isEmpty(dateValue))

        return null;

      SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);

      try {

        return dateFormat.parse(dateValue);

      } catch (ParseException pe) {

        return null;

      }

  }

  /**

    * 将Timestamp类型的日期转换为系统参数定义的格式的字符串。

    *

    * @param aTs_Datetime

    *            需要转换的日期。

    * @return 转换后符合给定格式的日期字符串

    */

  public static String formatDate(Date d) {

      return format(d, DATE_PATTERN_DEFAULT);

  }

  public static String formatDate(Timestamp d) {

      return format(d, DATE_PATTERN_DEFAULT);

  }



  public static String formatDateddmmss(Date d) {

      return format(d, TIME_PATTERN_DEFAULT);

  }


  /**

    * 将Timestamp类型的日期转换为系统参数定义的格式的字符串(只有数字)。

    *

    * @param aTs_Datetime

    *            需要转换的日期。

    * @return 转换后符合给定格式的日期字符串

    */

  public static String formatTimeTargetStr(Date t) {

      String timestr = null;

      timestr = format(t, TIME_PATTERN_DEFAULT_STR);

      return timestr;

  }

  /**

    * 将Timestamp类型的日期转换为系统参数定义的格式的字符串(只有数字)。

    *

    * @param aTs_Datetime

    *            需要转换的日期。

    * @return 转换后符合给定格式的日期字符串

    */

  public String formatTimeStr(Date t) {

      String timestr = null;

      synchronized (this) {

        timestr = format(t, TIME_PATTERN_DEFAULT_STR);

      }

      return timestr;

  }

  /**

    * 豪秒 将Timestamp 类型的日期转换为系统参数定义的格式的字符串(只有数字)。

    *

    * @param aTs_Datetime

    *            需要转换的日期。

    * @return 转换后符合给定格式的日期字符串

    */

  public String formatTimeSSSSStr(Date t) {

      String timestr = null;

      synchronized (this) {

        timestr = format(t, TIME_PATTERN_SSSS_STR);

      }

      return timestr;

  }

  /**

    * 将Timestamp类型的日期转换为系统参数定义的格式的字符串。

    *

    * @param aTs_Datetime

    *            需要转换的日期。

    * @return 转换后符合给定格式的日期字符串

    */

  public static String formatTime(Date t) {

      return format(t, TIME_PATTERN_DEFAULT);

  }

  /**

    * 将Date类型的日期转换为系统参数定义的格式的字符串。

    *

    * @param aTs_Datetime

    * @param as_Pattern

    * @return

    */

  public static String format(Date d, String pattern) {

      if (d == null)

        return null;

      SimpleDateFormat dateFromat = new SimpleDateFormat(pattern);

      return dateFromat.format(d);

  }


  /**

    * 将Timestamp类型的日期转换为系统参数定义的格式的字符串。

    *

    * @param t

    * @param pattern

    * @return

    */

  public static String format(Timestamp t, String pattern) {

      if (t == null)

        return null;

        Date date = new Date(t.getTime());


        return DateUtil.format(date, pattern);

  }


  /**

    * 将Timestamp类型的日期转换Date类型。

    *

    * @param t

    * @return

    */

  public static Date timestamp2Date(Timestamp t) {

      if (t == null)

        return null;

        return new Date(t.getTime());

  }

  /**

    * 取得指定日期N天后的日期

    *

    * @param date

    * @param days

    * @return

    */

  public static Date addDays(Date date, int days) {

      Calendar cal = Calendar.getInstance();

      cal.setTime(date);

      cal.add(Calendar.DAY_OF_MONTH, days);

      return cal.getTime();

  }



更详细的代码https://github.com/Shimmer8618/sunshine

你可能感兴趣的:(Java编写ip代理池(一))