刚从同事那里要来的,未验证。
场景:本地运行java执行定时发送短信,因公司代理无法正常连接网络,代码中设置代理相关配置即可。代码如下:
[code jang="java"]
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", PROXYHOST);
System.getProperties().put("proxyPort", PROXYPORT);
if ( StringUtils.isEmpty(PROXYUSER) && StringUtils.isEmpty(PROXYPWD) ){
Authenticator.setDefault(
new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
PROXYUSER, PROXYPWD.toCharArray());
}
}
);
System.getProperties().put("http.proxyUser", PROXYUSER);
System.getProperties().put("http.proxyPassword", PROXYPWD);
}
[/code]
mark下,需要的时候用的到。