> For the complete documentation index, see [llms.txt](https://docs.ipwo.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ipwo.net/kai-fa-zhe-wen-dang/dai-ma-shi-li/java.md).

# Java

使用 Java 可以通过 [IPWO 代理](https://www.ipwo.net/)发起 HTTP 请求，适用于 API 服务、数据采集与企业级应用场景。

***

### 一、HTTP 代理示例

```java
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();
        }
    }
}
```

***

### 二、正常返回示例

```json
{
  "ip": "203.0.113.10",
  "country": "US"
}
```

如果返回的 IP：

* 不是本机公网 IP
* 国家地区正确

说明代理已经生效。

***

### 三、增加 Timeout（推荐）

建议增加连接超时：

```java
connection.setConnectTimeout(30000);

connection.setReadTimeout(30000);
```

避免请求长时间卡住。

***

### 四、常见问题

#### [407 Proxy Authentication Required](/kai-fa-zhe-wen-dang/chang-jian-cuo-wu/407-proxy-authentication-required.md)

通常是：

* 用户名错误
* 密码错误
* zone 参数错误

***

#### [Timeout 超时](/kai-fa-zhe-wen-dang/chang-jian-cuo-wu/chao-shi-timeout.md)

通常是：

* 网络环境异常
* DNS 问题
* 请求超时时间过短

***

#### [Connection reset](/kai-fa-zhe-wen-dang/chang-jian-cuo-wu/connection-reset.md)

可能出现：

```
连接被远程服务器中断
```

建议：

* 增加 Retry
* 控制请求频率

***

### 五、推荐开发建议

推荐：

* 使用海外 VPS
* 增加 timeout
* 增加 Retry 重试机制
* 使用粘性 Session

适用于：

* 企业 API 服务
* 数据采集
* 自动化系统
* 高并发请求

***

### 六、相关文章

* [Go](/kai-fa-zhe-wen-dang/dai-ma-shi-li/go.md)
* [Python Requests](/kai-fa-zhe-wen-dang/dai-ma-shi-li/python/requests.md)
* [Node.js Axios](/kai-fa-zhe-wen-dang/dai-ma-shi-li/node.js.md#yi-axios)
* [API 调用与批量采集](/dai-li-ji-chu-zhi-shi/api-diao-yong-yu-pi-liang-cai-ji.md)
* [超时 Timeout](/kai-fa-zhe-wen-dang/chang-jian-cuo-wu/chao-shi-timeout.md)
* [什么是粘性会话](/dai-li-ji-chu-zhi-shi/shen-me-shi-nian-xing-hui-hua.md)
