Skip to content

Instantly share code, notes, and snippets.

@hihanley
Last active September 5, 2023 10:56
Show Gist options
  • Save hihanley/273afa9ba20420bad918275075981240 to your computer and use it in GitHub Desktop.
Save hihanley/273afa9ba20420bad918275075981240 to your computer and use it in GitHub Desktop.
Java okhttp 请求设置代理
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import okhttp3.OkHttpClient;
import okhttp3.Request;
public class Demo{
public static void main(String[] args) throws IOException {
var proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 1080));
var client = new OkHttpClient.Builder().proxy(proxy).build();
var request = new Request.Builder().url("https://ipinfo.io/json").get().build();
System.out.println(client.newCall(request).execute().body().string());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment