分享页面

隧道代理 Java 语言代码示例

package com.qgproxy;

import okhttp3.*;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.concurrent.TimeUnit;

public class QGProxy {
    final static String proxyIp = "隧道地址";
    final static Integer proxyPort = 隧道端口;
    final static String authKey = "AuthKey";
    final static String password = "AuthPwd";

    public Response request() throws IOException {
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyIp, proxyPort));
        OkHttpClient client = new OkHttpClient.Builder()
        proxy(proxy)

        proxyAuthenticator((route, response) -> {
            String credential = Credentials.basic(authKey, password);
            return response.request().newBuilder().header("Proxy-Authorization", credential).build();
        }.

        build();
        Request request = new Request.Builder().url("https://api.ipify.org").get().build();
        return client.newCall(request).execute();
    }

    public static void main(String[] args) {
        QGProxy qgProxy = new QGProxy();

        try {
            Response resp = qgProxy.request();
            System.out.println(resp.body().string());
        } 

        catch (Exception e) {
            System.out.printf("failed to proxy: %s\n", e.getMessage());
        }
    }
}
相关文章
本文导读