//创建代理服务器
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("www.proxyaddress.com", 8080));
        //设置代理的用户名密码
        Authenticator.setDefault(new MyAuth("用户名", "密码"));
        // 设定连接的相关参数
        URL url = new URL(locationUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);

    static class MyAuth extends Authenticator
    {
        private String user;
        private String pass;
        
        public MyAuth(String user, String pass)
        {
            this.user  = user;
            this.pass = pass;
        }

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return  new PasswordAuthentication(user, pass.toCharArray());
        }
    }

 

相关文章:

  • 2021-09-16
  • 2022-12-23
  • 2021-12-27
  • 2021-12-28
  • 2021-05-22
  • 2022-02-11
  • 2022-12-23
猜你喜欢
  • 2021-07-05
  • 2022-12-23
  • 2022-12-23
  • 2021-09-11
  • 2022-12-23
  • 2022-12-23
  • 2021-10-22
相关资源
相似解决方案