在请求类中设置代理
比如你使用的是OkHttpClient
:
new OkHttpClient.Builder()
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("ip","port")))
.build();
比如你使用的是Hutools
:
HttpRequest.post("https://api.openai.com/v1/completions")
.setHttpProxy("127.0.0.1", 10809)
.execute()
.body();
一般来讲,客户端工具都会提供设置Proxy
的入口。
在系统变量中设置代理
// 为http请求设置
System.setProperty("http.proxyHost", "127.0.0.1");
System.setProperty("http.proxyPort", "10809");
// 为https请求设置
System.setProperty("https.proxyHost", "127.0.0.1");
System.setProperty("https.proxyPort", "10809");
在程序启动前设置代理
其实这个和在系统变量中设置代理
是一个意思,都是给VM Options
设置参数,只是把变量赋值的操作提前了。
比如你使用的是idea
开发,那么可以给启动类(Run/Debug Configuration
)配置VM Options
参数:
-Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=10809
– end –