nashorn,通过代理使用身份验证的http请求

eoigrqb6  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(321)

我正在尝试使用nashorn(javascript)通过代理执行http/https请求。
到目前为止,我收到这个错误:

java.lang.RuntimeException: java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407 Proxy Authentication Required"<br/>at jdk.nashorn.internal.runtime.ScriptRuntime.apply

这是我的密码,请告诉我我做错了什么。

var System = Java.type("java.lang.System");
var JAuthenticator = Java.type("java.net.Authenticator");
var ProxyAuthenticator = Java.extend(JAuthenticator);
var ProxyAuthenticator = (function() {
    var 
        ProxyAuthenticator = Java.extend(JAuthenticator),
        _getPasswordAuthentication = function() { 
            return new java.net.PasswordAuthentication("user", "pwd".split(''));
        };
    return function(u, p) {
        return new ProxyAuthenticator() {
            getPasswordAuthentication : _getPasswordAuthentication
        };
    };
})();
System.setProperty("proxySet", "true");
System.setProperty("http.proxyHost", "proxy1");
System.setProperty("http.proxyPort", "8080");
System.setProperty("http.proxyUser", "user");
System.setProperty("http.proxyPassword", "pwd");
System.setProperty("https.proxyHost", "proxy1");
System.setProperty("https.proxyPort", "8080");
System.setProperty("https.proxyUser", "user");
System.setProperty("https.proxyPassword", "pwd");
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
System.setProperty("jdk.http.auth.proxying.disabledSchemes","");
System.setProperty("jdk.https.auth.tunneling.disabledSchemes", "");
System.setProperty("jdk.https.auth.proxying.disabledSchemes","");

JAuthenticator.setDefault(new ProxyAuthenticator());

httpGet("https://google.com");

//functions
function httpGet(theUrl){

    var con = new java.net.URL(theUrl).openConnection();
    con.requestMethod = "GET";
    return asResponse(con);
}
function asResponse(con){
    var d = read(con.inputStream);
    return {data : d, statusCode : con.responseCode};
}
function read(inputStream){
    var inReader = new java.io.BufferedReader(new java.io.InputStreamReader(inputStream));
    var inputLine;
    var response = new java.lang.StringBuffer();
    while ((inputLine = inReader.readLine()) != null) {
           response.append(inputLine);
    }
    inReader.close();
    return response.toString();
}

非常感谢你。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题