海外美国socks5 proxy 静态ip代理 购买

常见问题

2021-12-10 05:22:47

Java语言接入调用socks5/http代理IP

分享到:
0

Java语言接入使用socks5/http代理IP


java Java语言接入使用socks5/http代理IP 代码


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://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);

    }


    public static void testSocks5WithOkHttp() 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);

    }

}






上一篇:易语言接入调用socks5/http代理IP
下一篇:常见问题