OkHttpClient 我使用的并不多,这里记录下怎么给 OkHttpClient 添加代理,首先还是添加 OkHttpClient 依赖
1 2 3 4 5
| <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.10.0</version> </dependency>
|
API接入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package demo;
import okhttp3.OkHttpClient; import okhttp3.Request;
import java.io.IOException; import java.net.InetSocketAddress; import java.net.Proxy;
class ApiProxyJava { public static void main(String[] args) throws IOException { testHttpWithOkHttp(); testSocks5WithOkHttp(); }
public static void testHttpWithOkHttp() throws IOException { String url = "https://www.google.com/"; Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(IP, PORT)); OkHttpClient client = new OkHttpClient().newBuilder().proxy(proxy).build();
Request request = new Request.Builder().url(url).build(); okhttp3.Response response = client.newCall(request).execute(); String responseString = response.body().string(); System.out.println(responseString); response.close(); }
public static void testSocks5WithOkHttp() throws IOException { String url = "https://www.google.com/"; Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(IP, PORT)); OkHttpClient client = new OkHttpClient().newBuilder().proxy(proxy).build();
Request request = new Request.Builder().url(url).build(); okhttp3.Response response = client.newCall(request).execute(); String responseString = response.body().string(); System.out.println(responseString); response.close(); }
|
账密接入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| package demo;
import okhttp3.Credentials; import okhttp3.OkHttpClient; import okhttp3.Request;
import java.io.IOException; import java.net.InetSocketAddress; import java.net.PasswordAuthentication; import java.net.Proxy;
class AutProxyJava { public static void main(String[] args) throws IOException { testWithOkHttp();
testSocks5WithOkHttp(); }
public static void testWithOkHttp() throws IOException { String url = "https://www.google.com/"; String gateIp = "" int gatePort = 1000 Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(gateIp, gatePort)); OkHttpClient client = new OkHttpClient().newBuilder().proxy(proxy).proxyAuthenticator((route, response) -> { String credential = Credentials.basic("账户", "密码"); return response.request().newBuilder() .header("Proxy-Authorization", credential) .build(); }).build();
Request request = new Request.Builder().url(url).build(); okhttp3.Response response = client.newCall(request).execute(); String responseString = response.body().string(); System.out.println(responseString); response.close(); }
public static void testSocks5WithOkHttp() throws IOException { String url = "https://www.google.com/"; String gateIp = "" int gatePort = 2000 Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(gateIp, gatePort)); java.net.Authenticator.setDefault(new java.net.Authenticator() { private PasswordAuthentication authentication = new PasswordAuthentication("账户", "密码".toCharArray());
@Override protected PasswordAuthentication getPasswordAuthentication() { return authentication; } }); OkHttpClient client = new OkHttpClient().newBuilder().proxy(proxy).build(); Request request = new Request.Builder().url(url).build(); okhttp3.Response response = client.newCall(request).execute(); String responseString = response.body().string(); System.out.println(responseString); response.close(); } }
|
原文地址:https://admin.rola-ip.co/v_manual.html#/api_java