技术支持发布日期:2021-07-14
HttpAuthProxy
import okhttp3.Credentials;
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 HttpAuthProxy {
public static void main(String[] args) throws IOException {
testWithOkHttp();
}
public static void testWithOkHttp() throws IOException {
String url = "https://api.myip.la/en?json";
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("代理地址", 8080));
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);
}
}
HttpProxy
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 HttpProxy {
public static void main(String[] args) throws IOException {
testWithOkHttp();
}
public static void testWithOkHttp() throws IOException {
String url = "https://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);
}
}
Socks5AuthProxy
import okhttp3.OkHttpClient;
import okhttp3.Request;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
/**
* <p>
* compile 'com.squareup.okhttp3:okhttp:3.10.0'
*/
class Socks5AuthProxy {
public static void main(String[] args) throws IOException {
testWithOkHttp();
}
public static void testWithOkHttp() throws IOException {
String url = "https://api.myip.la/en?json";
Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("代理地址", 8080));
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);
}
}
Socks5Proxy
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 Socks5Proxy {
public static void main(String[] args) throws IOException {
testWithOkHttp();
}
public static void testWithOkHttp() throws IOException {
String url = "https://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);
}
}
Java语言客户,参考如下demo案例:
专属客服竭诚为您服务