Java 给 OkHttpClient 添加 Socks 代理

知了小站
2023-03-06 / 0 评论 / 411 阅读

OkHttpClient 我使用的并不多,这里记录下怎么给 OkHttpClient 添加代理,首先还是添加 OkHttpClient 依赖

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>3.10.0</version>
</dependency>

API接入

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();
    }

账密接入

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;

/**
 * compile 'com.squareup.okhttp3:okhttp:3.10.0'
 */
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

本文共 45 个字数,平均阅读时长 ≈ 1分钟
1

打赏

评论 (0)

取消