我正在通过代理发送一个http请求,需要在我的请求中添加用户名和密码。如何正确将这些值添加到我的选项块?
这是我的代码:
var http = require('http');
var options = {
port: 8080,
host: 'my.proxy.net',
path: '/index',
headers: {
Host: "http://example.com"
}
};
http.get(options, function(res) {
console.log("StatusCode: " + res.statusCode + " Message: " + res.statusMessage);
});
当前的响应是StatusCode:307,消息:Authentication Required。
我试图将用户名和密码添加到我的选项中,但是它不起作用:
var options = {
port: 8080,
host: 'my.proxy.net',
username: 'myusername',
password: 'mypassword',
path: '/index',
headers: {
Host: "http://example.com"
}
};
附加信息:我没有太多有关代理的信息,但在另一种情况下,此身份验证方法有效:
npm config set proxy http://username:[email protected]:8080
好的,这适用于我的本地鱿鱼:
var http = require('http');
function buildAuthHeader(user, pass) {
return 'Basic ' + new Buffer(user + ':' + pass).toString('base64');
}
proxy = 'localhost';
proxy_port = 3128;
host = 'www.example.com';
url = 'http://www.example.com/index.html';
user = 'potato';
pass = 'potato';
var options = {
port: proxy_port,
host: proxy,
path: url,
headers: {
Host: host,
'Proxy-Authorization': buildAuthHeader(user, pass),
}
};
http.get(options, function(res) {
console.log("StatusCode: " + res.statusCode + " Message: " + res.statusMessage);
});
夫妻注意事项:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句