gpt4 book ai didi

javascript - 我如何使用和授权带有 node.js http.Client 的 http 代理

转载 作者:搜寻专家 更新时间:2023-10-31 22:48:08 24 4
gpt4 key购买 nike

我正在通过代理发送一个 http 请求,需要在我的请求中添加用户名和密码。如何正确地将这些值添加到我的选项 block 中?

这是我的代码:

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, Message: 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:password@my.proxy.net: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);
});

注意事项:

  1. 完整的 URL 必须包含在 GET 行中,而不仅仅是路径,所以它不是/index.html 而是 http://example.com/index.html
  2. 您还应该在主机 header 中包含主机,这样您就必须正确解析 URL

关于javascript - 我如何使用和授权带有 node.js http.Client 的 http 代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29970728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com