Java语言如何接品易海外HTTP demo小课堂

技术支持发布日期:2021-07-14

1615363641811911.png

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案例:

Java语言下载教程


挂件 关闭
客服
二维码
客服二维码

加微信 领流量

大客户经理二维码

售前咨询,企业定制

专属客服竭诚为您服务