简体   繁体   English

为什么我会收到“Error socket hang up”响应?

[英]Why am I getting “Error socket hang up” response?

I tried to scrape a website.我试图抓取一个网站。 And I am using the library https://github.com/request/request , but I get this error:我正在使用库https://github.com/request/request ,但出现此错误:

Error: socket hang up
    at createHangUpError (http.js:1476:15)
    at Socket.socketOnEnd (http.js:1572:23)
    at Socket.g (events.js:180:16)
    at Socket.emit (events.js:117:20)
    at _stream_readable.js:943:16
    at process._tickDomainCallback (node.js:463:13) +0ms

What is wrong here?这里有什么问题? I also tried to re-install the package, but I get same error... help is appreciated.我也尝试重新安装该软件包,但我遇到了同样的错误......感谢帮助。

I recently got this socket hang up problem.我最近遇到了这个socket hang up问题。 I researched for a few days until I found a solution that worked for me.我研究了几天,直到找到适合我的解决方案。 So, maybe this can help.所以,也许这会有所帮助。

It worked for me to add the http(s)Agent property with keepAlive: true in creating the http client.在创建 http 客户端时使用keepAlive: true添加http(s)Agent属性对我keepAlive: true Here's an example of what it might look like:下面是它可能是什么样子的示例:

const http = require('http')
http.request({
        method: 'GET',
        agent: http.Agent({keepAlive:true}),
        host: 'www.google.com',
    })

This property is responsible for managing the sockets for client-side http connections.此属性负责管理客户端 http 连接的套接字。 The justification for using this property can be found here .可以在此处找到使用此属性的理由。

I also found an answer on the stackoverflow that also covers the subject, here .我还在 stackoverflow 上找到了一个答案,该答案也涵盖了该主题, here

So, I hope this helps.所以,我希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM