For the complete documentation index, see llms.txt. This page is also available as Markdown.

Java

使用 Java 可以通过 IPWO 代理发起 HTTP 请求,适用于 API 服务、数据采集与企业级应用场景。


一、HTTP 代理示例

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.util.Base64;

public class Main {

    public static void main(String[] args) throws Exception {

        try {

            String proxyHost = "us.ipwo.net";

            int proxyPort = 7878;

            Proxy proxy = new Proxy(
                Proxy.Type.HTTP,
                new InetSocketAddress(proxyHost, proxyPort)
            );

            String username =
                "username_custom_zone_us";

            String password = "password";

            String credentials =
                username + ":" + password;

            URL url =
                new URL("http://ipinfo.io");

            HttpURLConnection connection =
                (HttpURLConnection)
                url.openConnection(proxy);

            connection.setRequestMethod("GET");

            connection.setRequestProperty(
                "Proxy-Authorization",
                "Basic " +
                Base64.getEncoder()
                    .encodeToString(
                        credentials.getBytes()
                    )
            );

            int responseCode =
                connection.getResponseCode();

            System.out.println(
                "Response Code: " +
                responseCode
            );

            BufferedReader in =
                new BufferedReader(
                    new InputStreamReader(
                        connection.getInputStream()
                    )
                );

            String inputLine;

            StringBuilder response =
                new StringBuilder();

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

                response.append(inputLine);
            }

            in.close();

            System.out.println(
                response.toString()
            );

        } catch (IOException e) {

            e.printStackTrace();
        }
    }
}

二、正常返回示例

如果返回的 IP:

  • 不是本机公网 IP

  • 国家地区正确

说明代理已经生效。


三、增加 Timeout(推荐)

建议增加连接超时:

避免请求长时间卡住。


四、常见问题

通常是:

  • 用户名错误

  • 密码错误

  • zone 参数错误


通常是:

  • 网络环境异常

  • DNS 问题

  • 请求超时时间过短


可能出现:

建议:

  • 增加 Retry

  • 控制请求频率


五、推荐开发建议

推荐:

  • 使用海外 VPS

  • 增加 timeout

  • 增加 Retry 重试机制

  • 使用粘性 Session

适用于:

  • 企业 API 服务

  • 数据采集

  • 自动化系统

  • 高并发请求


六、相关文章

最后更新于

这有帮助吗?