| | |
| | | package com.yeshi.fanli.util;
|
| | |
|
| | | import java.util.ArrayList;
|
| | | import java.util.List;
|
| | |
|
| | | import net.sf.json.JSONArray;
|
| | | import net.sf.json.JSONObject;
|
| | |
|
| | | import com.yeshi.fanli.entity.address.Address;
|
| | |
|
| | | public class ProxyUtil {
|
| | | |
| | | private static final String url="http://118.190.209.189:8080/nv/api/client/ip/getproxyips";
|
| | | |
| | | package com.yeshi.fanli.util; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | import net.sf.json.JSONArray; |
| | | import net.sf.json.JSONObject; |
| | | |
| | | import com.yeshi.fanli.entity.address.Address; |
| | | |
| | | public class ProxyUtil { |
| | | |
| | | private static final String url="http://118.190.209.189:8080/nv/api/client/ip/getproxyips"; |
| | | |
| | | private static final List<Address> addressPool = new ArrayList<Address>(); |
| | | |
| | | private volatile static int pointer=0; //指针
|
| | | |
| | | public static void updateProxyPool(){
|
| | | try{
|
| | | String ips = TaoBaoHttpUtil.get(url,false);
|
| | | JSONArray array = JSONArray.fromObject(ips);
|
| | | Address address = null;
|
| | | int size = addressPool.size();
|
| | | for (Object obj : array) {
|
| | | JSONObject data = (JSONObject)obj;
|
| | | String ip = data.optString("ip");
|
| | | int port = Integer.parseInt(data.optString("port"));
|
| | | address = new Address(ip,port);
|
| | | addressPool.add(address);
|
| | | }
|
| | | for(int i=0;i<size-1;i++){
|
| | | addressPool.remove(i);
|
| | | }
|
| | | }catch(Exception e){
|
| | | System.out.println("代理更新失败");
|
| | | }
|
| | | }
|
| | | |
| | | public static Address getAddressProxy(){
|
| | | int size = addressPool.size();
|
| | | if(size==0){
|
| | | return null;
|
| | | }
|
| | | if(size-2 >= pointer){
|
| | | pointer++;
|
| | | }else{
|
| | | pointer=0;
|
| | | }
|
| | | return addressPool.get(pointer);
|
| | | }
|
| | | }
|
| | |
|
| | | |
| | | private volatile static int pointer=0; //指针 |
| | | |
| | | public static void updateProxyPool(){ |
| | | try{ |
| | | String ips = TaoBaoHttpUtil.get(url,false); |
| | | JSONArray array = JSONArray.fromObject(ips); |
| | | Address address = null; |
| | | int size = addressPool.size(); |
| | | for (Object obj : array) { |
| | | JSONObject data = (JSONObject)obj; |
| | | String ip = data.optString("ip"); |
| | | int port = Integer.parseInt(data.optString("port")); |
| | | address = new Address(ip,port); |
| | | addressPool.add(address); |
| | | } |
| | | for(int i=0;i<size-1;i++){ |
| | | addressPool.remove(i); |
| | | } |
| | | }catch(Exception e){ |
| | | System.out.println("代理更新失败"); |
| | | } |
| | | } |
| | | |
| | | public static Address getAddressProxy(){ |
| | | int size = addressPool.size(); |
| | | if(size==0){ |
| | | return null; |
| | | } |
| | | if(size-2 >= pointer){ |
| | | pointer++; |
| | | }else{ |
| | | pointer=0; |
| | | } |
| | | return addressPool.get(pointer); |
| | | } |
| | | } |
| | | |