java使用代理服务器访问网络

2 篇文章 0 订阅
1 篇文章 0 订阅

最近公司用代理 取数据遇到问题

具体解决如下

Properties prop = System.getProperties();
//设置http访问要使用的代理服务器的地址
prop.setProperty("http.proxyHost", "10.10.0.96");
//设置http访问要使用的代理服务器的端口
prop.setProperty("http.proxyPort", "8080");
//设置不需要通过代理服务器访问的主机,可以使用*通配符,多个地址用|分隔
prop.setProperty("http.nonProxyHosts", "localhost|10.10.*");

//设置安全访问使用的代理服务器地址与端口
//它没有https.nonProxyHosts属性,它按照http.nonProxyHosts 中设置的规则访问

prop.setProperty("https.proxyHost", "10.10.0.96");
prop.setProperty("https.proxyPort", "443");

//使用ftp代理服务器的主机、端口以及不需要使用ftp代理服务器的主机
prop.setProperty("ftp.proxyHost", "10.10.0.96");
prop.setProperty("ftp.proxyPort", "2121");
prop.setProperty("ftp.nonProxyHosts", "localhost|10.10.*");

//socks代理服务器的地址与端口
prop.setProperty("socksProxyHost", "10.10.0.96");
prop.setProperty("socksProxyPort", "1080");

//设置登陆到代理服务器的用户名和密码
Authenticator.setDefault(new MyAuthenticator(uid, pwd));

class MyAuthenticator extends Authenticator {
    private String user = "";

    private String password = "";

    public MyAuthenticator(String user, String password) {
        this.user = user;
        this.password = password;
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, password.toCharArray());
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值