菜单

如何通过Java配置动态代理?

配置前请注意:Java需要在海外网络环境下执行才能使IPFoxy代理生效


一、 IPFoxy页面操作

购买动态代理后,进入【动态住宅代理】页面,完成以下操作:

1、协议类型:http

2、格式:Username:Password@Host:Port

3、生成代理

4、复制生成的连接信息


二、 在Java操作

1、 将以下代码复制到Java代码中,填写authUser和authPassword变量内容

import java.net.*;
import java.io.*;
import java.util.Scanner;


public class Main
{

    public static void main(String[] args) throws Exception
    {
        final String authUser = "username";
        final String authPassword = "password";
        InetSocketAddress proxyAddress = new InetSocketAddress("gate-us.ipfoxy.io", 58688); // Set proxy IP/port.
        Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyAddress);
        URL url = new URL("https://lumtest.com/myip.json"); //enter target URL
        Authenticator authenticator = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return (new PasswordAuthentication(authUser,authPassword.toCharArray())); //enter credentials
            }
        };
        System.setProperty("http.proxyUser", authUser);
        System.setProperty("http.proxyPassword", authPassword);
        System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");

        Authenticator.setDefault(authenticator);
        URLConnection urlConnection = url.openConnection(proxy);
        Scanner scanner = new Scanner(urlConnection.getInputStream());
        for (; ; ) {
            if (scanner.hasNext()) {
                System.out.print(scanner.next());
            }else{
                break;
            }
        }
        scanner.close();
    }
}


分享文章
上一个
如何通过PHP配置动态代理?
下一个
如何通过Go配置动态代理?
最近修改: 2024-11-28Powered by