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

使用教程发布日期:2021-01-26

Java语言如何接品易HTTP demo小课堂.png


Java语言客户,使用代理ip,参考如下demo案例


Java语言—API教程(下载)


Java语言—账密教程(下载)


Java语言—API


package demo;


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 ApiProxyJava {

    public static void main(String[] args) throws IOException {

        testHttpWithOkHttp();

        testSocks5WithOkHttp();

    }


    public static void testHttpWithOkHttp() throws IOException {

        String url = "http://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);

    }


    public static void testSocks5WithOkHttp() throws IOException {

        String url = "http://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语言—账密


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

    }

}

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

加微信 领流量

大客户经理二维码

售前咨询,企业定制

专属客服竭诚为您服务