神奇的是,使用 https 模块时可以正常访问 uri 并保存在本地,但是使用 request 模块时请求都发不出去,百思不得其解,报错信息如下:
Error: tunneling socket could not be established, cause=getaddrinfo ENOTFOUND 7890
at ClientRequest.onError (D:\www\BWK_Spider\node_modules\tunnel-agent\index.js:177:17)
at Object.onceWrapper (node:events:642:26)
at ClientRequest.emit (node:events:527:28)
at Socket.socketErrorListener (node:_http_client:454:9)
at Socket.emit (node:events:527:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'ECONNRESET'
}
request 模块代码:
const request = require("request");
const fs = require("fs");
request("https://xxx.xx.xx/20_C12100030.pdf")
.pipe(fs.createWriteStream("C:\\Users\\444\\Desktop\\20_C12100030.pdf"))
.on("close", function (err) {
console.log("下载完成");
console.log(err);
})
.on("error", function (err) {
console.log(err);
});
https 模块代码
const https = require("https");
const file = fs.createWriteStream(
"C:\\Users\\444\\Desktop\\20_C12100030.pdf"
);
https
.get("https://xxx.xx.xx/20_C12100030.pdf", (response) => {
console.log("请求成功");
response.pipe(file);
// after download completed close filestream
file.on("finish", () => {
file.close();
console.log("Download Completed");
});
})
.on("error", (err) => {
console.log("Error: ", err.message);
});
有遇到这种情况的吗?网上搜索都说是 npm 代理导致的,但实际不是,否则使用 https 模块时也应该是请求发不出去才对。
1
okakuyang 2022-12-15 17:59:44 +08:00
7890 这个显然是你的梯子代理端口
|
2
Pastsong 2022-12-15 18:02:02 +08:00
request 读了你的 HTTP_PROXY/HTTPS_PROXY 环境变量
|
3
s609926202 OP @okakuyang
我梯子可正常访问呐 |
4
Opportunity 2022-12-16 15:07:37 +08:00
https 模块又不会自动走代理
|