关键是设置系统属性

System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost", proxy[0]);
System.setProperty("http.proxyPort", proxy[1]);


直接上代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HttpProxy {
    private static String[] proxyArray = { 
            "120.52.72.48:80", 
            "110.52.232.60:80", 
            "117.135.251.133:80",
            "122.140.245.106:8000", 
            "119.255.9.93:80", 
            "117.135.252.227:80", 
            "120.198.231.22:8080", 
            "117.158.149.34:80",
            "183.239.167.122:8080" };

    public static void main(String[] args) throws Exception {
        final String spec = "http://1212.ip138.com/ic.asp";
        final String charset = "GB2312";

        System.out.println("不使用代理");
        parse(sendHttpPost(spec, charset));

        for (int i = 0; i < proxyArray.length; i++) {
            try {
                String[] proxy = proxyArray[i].split(":");
                System.setProperty("http.proxySet", "true");
                System.setProperty("http.proxyHost", proxy[0]);
                System.setProperty("http.proxyPort", proxy[1]);
                System.out.println("代理IP是: " + proxyArray[i]);
                parse(sendHttpPost(spec, charset));
            }
            catch (Throwable t) {
                System.out.println(t);
            }
        }
    }

    private static void parse(String result) {
        Pattern pattern = Pattern.compile("
(.*)
");         Matcher matcher = pattern.matcher(result);         while (matcher.find()) {             System.out.println(matcher.group(1));         }         System.out.println();     }     private static String sendHttpPost(String spec, String charset) throws MalformedURLException, IOException {         URL url = new URL(spec);         URLConnection connection = url.openConnection();         HttpURLConnection httpURLConnection = (HttpURLConnection) connection;         StringBuffer resultBuffer = new StringBuffer();         if (httpURLConnection.getResponseCode() == 200) {             try (BufferedReader reader = new BufferedReader(                     new InputStreamReader(httpURLConnection.getInputStream(), charset))) {                 String tempLine = null;                 while ((tempLine = reader.readLine()) != null) {                     resultBuffer.append(tempLine);                 }             }         }         return resultBuffer.toString();     } }

输出

不使用代理
您的IP是:[183.15.246.30] 来自:广东省深圳市 电信

代理IP是: 120.52.72.48:80
您的IP是:[221.221.163.195] 来自:北京市北京市 联通

代理IP是: 110.52.232.60:80
您的IP是:[183.15.246.30] 来自:广东省深圳市 电信

代理IP是: 117.135.251.133:80
您的IP是:[117.135.251.133] 来自:贵州省 移动

代理IP是: 122.140.245.106:8000
您的IP是:[183.15.246.30] 来自:广东省深圳市 电信

代理IP是: 119.255.9.93:80
您的IP是:[183.15.246.30] 来自:广东省深圳市 电信

代理IP是: 117.135.252.227:80
您的IP是:[117.135.252.227] 来自:贵州省 移动

代理IP是: 120.198.231.22:8080
您的IP是:[120.198.231.22] 来自:广东省东莞市 移动

代理IP是: 117.158.149.34:80
您的IP是:[183.15.246.30] 来自:广东省深圳市 电信

代理IP是: 183.239.167.122:8080
您的IP是:[183.239.167.122] 来自:广东省深圳市 移动