Nodejs request 模块和 https 模块请求同一个文件 uri 时, request 模块请求失败 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
s609926202
V2EX    Node.js

Nodejs request 模块和 https 模块请求同一个文件 uri 时, request 模块请求失败

  •  
  •   s609926202 2022-12-15 17:53:41 +08:00 5102 次点击
    这是一个创建于 1104 天前的主题,其中的信息可能已经有所发展或是发生改变。

    神奇的是,使用 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 模块时也应该是请求发不出去才对。

    4 条回复    2022-12-16 15:07:37 +08:00
    okakuyang
        1
    okakuyang  
       2022-12-15 17:59:44 +08:00
    7890 这个显然是你的梯子代理端口
    Pastsong
        2
    Pastsong  
       2022-12-15 18:02:02 +08:00
    request 读了你的 HTTP_PROXY/HTTPS_PROXY 环境变量
    s609926202
        3
    s609926202  
    OP
       2022-12-16 10:40:45 +08:00
    @okakuyang
    我梯子可正常访问呐
    Opportunity
        4
    Opportunity  
       2022-12-16 15:07:37 +08:00
    https 模块又不会自动走代理
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     955 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 22:32 PVG 06:32 LAX 14:32 JFK 17:32
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86