![](/img/trans.png)
[英]Java HttpsURLConnection via Proxy not working while wget works fine
[英]java use jvm proxy setting still returns 407 while postman works fine
它的工作原理只是找到,可以获得我的目标url内容,而在intellij中,代码如下所示,
public static void main(String[] args) throws IOException, InterruptedException {
System.setProperty("java.net.useSystemProxies", "true");
String url = "http://xxxx/";
String charset = "UTF-8";
URLConnection connection = new URL(url).openConnection();
connection.setRequestProperty("Accept-Charset", charset);
InputStream response = connection.getInputStream();
String result = new BufferedReader(new InputStreamReader(response))
.lines().collect(Collectors.joining("\n"));
System.out.println(result);
}
我还添加了我的 jvm 参数,例如
-Dhttp.proxyUser=username -Dhttp.proxyPassword=password -Dhttp.proxyHost=proxyhost -Dhttp.proxyPort=8080
但上面的代码一直说:服务器返回 HTTP 响应代码:407 for URL
有人可以告诉我我在这里错过的任何事情吗?
从HTTP Error 407 Proxy authentication required中找到答案并且它有效,需要添加以下代码
Authenticator.setDefault(new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("username","password".toCharArray());
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.