JAVA
Example
package demo;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
/**
* compile 'com.squareup.okhttp3:okhttp:3.10.0'
*/
class ApiProxyJava {
public static void main(String[] args) throws IOException {
testHttpWithOkHttp();
testSocks5WithOkHttp();
}
public static void testHttpWithOkHttp() throws IOException {
String url = "http://api.myip.la/en?json";
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("ip", 2000));
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);
}
public static void testSocks5WithOkHttp() throws IOException {
String url = "http://api.myip.la/en?json";
Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("ip", 8080));
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);
}
}
Last updated