Why do I get “Errno::ECONNREFUSED” with 'net/http' in Rails?

对着背影说爱祢 提交于 2019-12-05 07:51:39

Net::HTTP is part of Ruby's standard library. You can use require 'net/http' to load it.

Rather than use Net::HTTP, which is fairly low-level for what you want to do, I'd recommend using Ruby's Open::URI.

If you are moving a lot of HTTP data, you might want to look into something like HTTPClient or Curb or Typhoeus, which are designed for heavy-weight use and save you the trouble of having to write it all using Net::HTTP.

Regarding the ECONNREFUSED error: you might want to capture the HTTPResponse returned from get_response, then check its status before trying to read the body. The way you're doing it now doesn't let you react to a failed request.

Looks like the problem is that you can't connect to the HTTP-server.
You could try checking that you can access the URL in a browser.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!