admin
2019-10-10 e19ce4be094d93f68bdb6ee1c28e9caa502bf2c4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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);
    }
}