javascript - nashorn,使用身份验证通过代理的http请求
问题描述
我正在尝试使用 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();
}
非常感谢。
解决方案
推荐阅读
- python - 变分自动编码器 (VAE) 返回一致的输出
- rebol - 如何使用默认参数构造函数
- moodle-api - 从其他数据库创建用户,其中 php-script
- python - 为什么 RegExr.com 显示的结果与我得到的结果不同?
- sql - SQL - 使用大量未排序的过滤器进行过滤
- r - 打印一个循环通过 3 个参数和输出匹配条件的 R 函数
- swiftui - ScrollView 内容的动态高度不起作用
- r - 如何在 R 中存储具有当前日期/Sys.Date() 的文件?
- python - 如何使用 OpenCV 在视频上绘制 matplotlib 图
- mongodb - 执行文件删除操作的最佳位置在哪里?