从Docker容器到LAN IP的HTTPS请求失败,但是curl起作用
我正在开发可在我的局域网上访问的embedded式设备。 我想从包含我的开发环境的Docker容器中向它的API发出一个请求。
该设备暴露于https://172.16.1.92
(当然,它没有正确的HTTPS证书)。 我可以成功访问我的浏览器中的这个URL,一切正常。
如果我运行这个:
docker run -it --rm node:5 curl --insecure https://172.16.1.92/api
我看到了预期的结果(一个JSON对象)。
但是,编写一个快速的JStesting脚本之后:
var https = require('https'); https.request({ host: '172.16.1.92', method: 'GET', path: '/api', timeout: 5 * 1000, rejectUnauthorized: false, // for the lack of certificate }, function(res) { console.log(res.statusCode); });
并运行它:
docker run -it --rm -v `pwd`:/code -w /code node:5 node test.js
几秒钟后我得到这个错误:
events.js:141 throw er; // Unhandled 'error' event ^ Error: socket hang up at createHangUpError (_http_client.js:206:15) at TLSSocket.socketOnEnd (_http_client.js:298:23) at emitNone (events.js:72:20) at TLSSocket.emit (events.js:166:7) at endReadableNT (_stream_readable.js:905:12) at nextTickCallbackWith2Args (node.js:474:9) at process._tickCallback (node.js:388:17)
我很困惑 – 为什么我可以使用curl访问URL,但不使用节点的https
库?
如果我在主机上运行nmap -sP 172.16.1.*
,我看到这一行:
Nmap scan report for WRN_002258.lan (172.16.1.92) Host is up (0.0011s latency). MAC Address: xx:xx:xx:xx:xx:xx (vendor)
但是,如果我在Docker容器中安装nmap
并运行相同的命令,我可以看到:
Nmap scan report for 172.16.1.92 Host is up (0.0032s latency).
这很重要吗?
您的代码与HTTP服务器执行TCP握手,但不会通过它发送数据。
修改你的Node.js代码来:
var https = require('https'); var req = https.request({ host: '172.16.1.92', method: 'GET', path: '/api', timeout: 5 * 1000, rejectUnauthorized: false, // for the lack of certificate }, function(res) { console.log(res.statusCode); }); req.end()
您应该阅读https.request()的文档和http.request()的文档。 一旦你这样做了,你最终也会阅读request.end()
对于TL; DR时刻: request.end()
完成发送请求。