Properties prop = System.getProperties();
System.getProperties().put("proxySet","true");
// 设置http访问要使用的代理服务器的地址
prop.setProperty("http.proxyHost","172.17.0.2");
// 设置http访问要使用的代理服务器的端口
prop.setProperty("http.proxyPort","8080");
httpURL = new URL(url);
System.out.println("-----------url:"+url+"--------------");
connection = (HttpURLConnection) httpURL.openConnection();
System.out.println("-----------httpURL.openConnection()--------------");
String authentication = "gaozhengyue:131421"; // 用户名密码
String encodedLogin = new BASE64Encoder().encode(authentication.getBytes()); // 编码
connection.setRequestProperty("Proxy-Authorization", " Basic " + encodedLogin); // 授权
System.out.println("-----------connection8.setRequestProperty:"+encodedLogin+"--------------");
利用httpURL 创建代理
最新推荐文章于 2024-06-04 13:45:14 发布
本文介绍如何在 Java 中配置 HTTP 代理并实现基本的身份验证。通过设置系统的属性来指定代理服务器的主机名和端口号,并使用 BASE64 编码对用户名密码进行编码以完成代理认证。
374

被折叠的 条评论
为什么被折叠?



