常见问题
产品介绍
代码示例

java示例代码

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.InetSocketAddress;
    import java.net.Proxy;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.ArrayList;
    import java.util.List;
        
    public class proxyHttpTest {
        public static List<String> ipList = new ArrayList<>();
    
        public static void main(String[] args) {
    
            //填写您的河马代理API提取接口地址,在http://www.hemadaili.com/extract/blend.html生成链接:
            getIp("http://api.hemadaili.com/proxy/extract?order=XXXX&count=1&pt=0&fmt=0&sep=2&dd=1");
    
            //您将要爬取的地址,这里使用http://ip.cip.cc/来检测代理是否启用成功
            getDocByJsoup("http://ip.cip.cc/");
    
        }
    
        public static String getDocByJsoup(String href) {
            String ipport = getAProxy();
            BufferedReader reader = null;
            String line;
            try {
    
                //获取短效IP
                URL url = new URL(href);
                System.out.println("代理IP服务器地址为:" + ipport);
                String ip = ipport.split(":")[0];
                int port = Integer.parseInt(ipport.split(":")[1]);
    
                //设置代理IP服务器
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port));
    
                URLConnection conn = (URLConnection) url.openConnection(proxy);
                conn.setConnectTimeout(5000);
    
                //爬取页面内容
                reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
                while ((line = reader.readLine()) != null) {
                    System.out.println("您本次访问使用的IP地址为:" + line);
                    if (ip.equals(line)) {
                        System.out.println("代理IP使用成功!");
                    }
                }
                return line;
            } catch (Exception e) {
                e.printStackTrace();
                return getDocByJsoup(href);
            }
        }
    
        //获取代理IP服务器信息,存入webParesTest.ipList对象
        public static void getIp(String api) {
            List<String> ipList = new ArrayList<>();
            BufferedReader reader = null;
            URL url = null;
            String line;
            try {
                String str = api;
                url = new URL(str);
                URLConnection conn = (URLConnection) url.openConnection();
                reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
                while ((line = reader.readLine()) != null) {
                    ipList.add(line);
                }
                if (ipList.size() > 0) {
                    proxyHttpTest.ipList = ipList;
                }
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        //从webParesTest.ipList对象随机获取一条代理IP
        private static String getAProxy() {
            if (ipList.size() > 0) {
                String ip = ipList.get((int) (Math.random() * ipList.size()));
                return ip;
            }
            return null;
        }
    }
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.InetSocketAddress;
    import java.net.Proxy;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.ArrayList;
    import java.util.List;
        
    public class proxySocks5Test {
        public static List<String> ipList = new ArrayList<>();
        public static void main(String[] args) {
    
            //填写您的河马代理API提取接口地址,在http://www.hemadaili.com/extract/blend.html生成链接:
            getIp("http://api.hemadaili.com/proxy/extract?order=XXXX&count=1&pt=0&fmt=0&sep=2&dd=1");
    
            //您将要爬取的地址,这里使用http://ip.cip.cc/来检测代理是否启用成功
            getDocByJsoup("http://ip.cip.cc/");
    
        }
    
        public static String getDocByJsoup(String href) {
            String ipport = getAProxy();
            BufferedReader reader = null;
            String line;
            try {
    
                //获取短效IP
                URL url = new URL(href);
                System.out.println("代理IP服务器地址为:" + ipport);
                String ip = ipport.split(":")[0];
                int port = Integer.parseInt(ipport.split(":")[1]);
    
                //设置socks5代理IP服务器
                Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(ip, port));
    
                URLConnection conn = (URLConnection) url.openConnection(proxy);
                conn.setConnectTimeout(5000);
    
                //爬取页面内容
                reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
                while ((line = reader.readLine()) != null) {
                    System.out.println("您本次访问使用的IP地址为:" + line);
                    if (ip.equals(line)) {
                        System.out.println("代理IP使用成功!");
                    }
                }
                return line;
            } catch (Exception e) {
                e.printStackTrace();
                return getDocByJsoup(href);
            }
        }
    
        //获取代理IP服务器信息,存入webParesTest.ipList对象
        public static void getIp(String api) {
            List<String> ipList = new ArrayList<>();
            BufferedReader reader = null;
            URL url = null;
            String line;
            try {
                String str = api;
                url = new URL(str);
                URLConnection conn = (URLConnection) url.openConnection();
                reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
                while ((line = reader.readLine()) != null) {
                    ipList.add(line);
                }
                if (ipList.size() > 0) {
                    proxySocks5Test.ipList = ipList;
                }
    
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        //从webParesTest.ipList对象随机获取一条代理IP
        private static String getAProxy() {
            if (ipList.size() > 0) {
                String ip = ipList.get((int) (Math.random() * ipList.size()));
                return ip;
            }
            return null;
        }
    }