想让java通过http代理连接到网络,怎么设置代理的域名,用户名,密码?关键是代理的域名,域名
- System.getProperties().put("http.proxySet", "true");
- System.getProperties().put("http.proxyHost", "host");
- System.getProperties().put("http.proxyPort", "port");
- Authenticator.setDefault(new DefaultAuthenticator());
- URL url = new URL("http://www.baidu.com");
- URLConnection conn = url.openConnection();
- //代理域名,用户名,密码设置如下:
- public class DefaultAuthenticator extends Authenticator {
- private static String username = "username";
- private static String domain = "domain";
- private static String password= "password";
- public PasswordAuthentication getPasswordAuthentication() {
- String userNameWithDomain = domain + "\\" + username;
- return (new PasswordAuthentication(userNameWithDomain, password.toCharArray()));
- }
- }