java 无法通过代理建立隧道。代理返回“需要 HTTP/1.1 407 代理授权”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17452956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:03:10  来源:igfitidea点击:

Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407 Proxy Authorization Required"

javahttpsnode-http-proxy

提问by rand

I was trying to hit the https url for google api. Using the code below but its giving some errors. but i can hitting one google api http url and its working very well without any error

我试图点击 google api 的 https url。使用下面的代码,但它给出了一些错误。但我可以点击一个 google api http url 并且它运行良好,没有任何错误

DataInputStream di = null;

FileOutputStream fo = null;

byte[] b = new byte[1];

// PROXY
System.setProperty("https.proxyHost", "my proxy");
System.setProperty("https.proxyPort", "8080");


Authenticator.setDefault(new PasswordAuthenticator());


URL u = new URL("https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Vict&types=geocode&language=fr&sensor=true&key=keyforuse");

HttpURLConnection con = (HttpURLConnection) u.openConnection();

sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
String encodedUserPwd = encoder.encode("domain\user:password"
        .getBytes());
con.setRequestProperty("Proxy-Authorization", "Basic "
        + encodedUserPwd);

di = new DataInputStream(con.getInputStream());
// result = parseJSON(di);
while (-1 != di.read(b, 0, 1)) {
    System.out.print(new String(b));
}

but getting error below

但在下面出现错误

java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407 Proxy Authorization Required"
    at sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:1648)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:164)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
    at kf.store.locator.googlemaps.test.main(test.java:58)

any help????

任何帮助????

回答by Callan

You may not need the domain in front of the username. I had a very similar issue, and I fixed it when I removed the domain in front of the username. Since the proxy server is on a domain, I think it implies that your under the same domain.

您可能不需要用户名前面的域。我有一个非常相似的问题,当我删除用户名前面的域时,我修复了它。由于代理服务器在一个域上,我认为这意味着您在同一个域下。

回答by Ishu Gupta

I fixed it by authenticating to the proxy server with my user name and password. If you are on your company's network , it can be your sso cred too. The equivalent of this will be like this :

我通过使用我的用户名和密码对代理服务器进行身份验证来修复它。如果您在公司的网络上,它也可以成为您的 sso 信誉。等价的将是这样的:

-Dhttp.proxyUser=myusername 
-Dhttp.proxyPassword=mypassword
-Dhttp.proxyHost=myproxyserver.com
-Dhttp.proxyPort=9000