Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to parse an XML file from a URL. When I try something like this:

require 'net/http'
require 'rubygems'
require 'xmlsimple'

url = 'http://my-address.com/xmltest/note.xml'
xml_data = Net::HTTP.get_response(URI.parse(url)).body

Everything works, but only when I do this outside of my Rails project. If I try including this file in my Rails 3 project and parsing it there, I get the error "Errno::ECONNREFUSED in [controller]" - Connection refused - connect(2).

My problem is the following: I don't know how to install the net/http component. I am looking for it on http://www.rubygems.org, but I can't find it.

like image 493
user1946705 Avatar asked Jun 03 '11 18:06

user1946705


People also ask

What is econnrefused – connection refused by server?

FileZilla is a popular, free, and open-source FTP, FTPS, and SFTP client used to upload and download files. “ECONNREFUSED – Connection refused by server” is one of the more common errors that users of FileZilla may encounter.

Why is my data connection econnrefused?

I have a different ECONNREFUSED message - "the data connection could not be established", is the fix the same as in your guide above? The main cause of “the data connection could not be established” message when using an FTP server lies in the settings of the FTP tool used to connect to the server from a remote machine.

How to fix “connection refused by server” FileZilla error messages?

If you’re looking for a quick fix to “ECONNREFUSED – Connection refused by server” FileZilla error messages, try changing your port number from 21 (default FTP port) to 22 (default SFTP port) or vice versa, as that is one of the most common fixes. Then try again to connect to the host IP address or domain you wish to access.

How long does it take for nodejs error connect econnrefused to work?

Here it's my success story with incredible persistant Node.js error connect ECONNREFUSED . Day 1. I run this code, and should work fine. Usual errors, maybe 3-4 minutes. I changed host: 'localhost' to host : '127.0.0.1', port: 8080, or maybe 8000, oh yes, 3306 this is.


1 Answers

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.

like image 130
the Tin Man Avatar answered Oct 21 '22 22:10

the Tin Man