3

我的一台服务器使用 net::http 连接到我的另一台服务器(在另一台机器上)。当我使用 curl 时,它可以工作(在本地主机上):

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"user":{"email":"info@mysite.com","password":"password"}}' http://mysite.com:80/pages/account_stuff.json
=> {"account_id":4,"account_name":"Some Name","status":200}

在本地主机上,一切正常。在我的一台服务器上,完全不同的场景。

当我在我的 ubuntu 服务器上使用 net::http 时,我得到 Errno::ECONNREFUSED: Connection denied - connect(2):

url = "http://mysite.com:80/pages/account_stuff.json"
@uri = URI.parse(url)
@body = {"user" => {"email" => "info@mysite.com", "password" => "password"}}.to_json
@headers = {'Content-Type' => 'application/json'}
req = Net::HTTP::Post.new(@uri.to_s, initheader = @headers)
req.body = @body
http = Net::HTTP.new(@uri.host,@uri.port).start
Errno::ECONNREFUSED: Connection refused - connect(2)
    from /usr/local/rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:762:in `initialize'
    from /usr/local/rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:762:in `open'
    from /usr/local/rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:762:in `block in connect'
    from /usr/local/rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/timeout.rb:54:in `timeout'
    from /usr/local/rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/timeout.rb:99:in `timeout'
    from /usr/local/rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:762:in `connect'
    from /usr/local/rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:755:in `do_start'
    from /usr/local/rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:750:in `start'
    from (irb):17
    from /home/mysite/public_html/store.mysite.com/shared/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
    from /home/mysite/public_html/store.mysite.com/shared/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
    from /home/mysite/public_html/store.mysite.com/shared/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

尝试了其他后期技术:

uri = URI.parse('http://mysite.com/pages/account_stuff.json')
res = Net::HTTP.post_form(uri, {"user" => {"email" => "info@mysite.com", "password" => "password"}})
Errno::ECONNREFUSED: Connection refused - connect(2)

然后尝试了 net::http 获取请求:

uri = URI('http://mysite.com/pages/account_stuff')
Net::HTTP.get(uri)
Errno::ECONNREFUSED: Connection refused - connect(2)

然后删除 net::http 并尝试使用 open::uri 代替:

open("http://mysite.com")
Errno::ECONNREFUSED: Connection refused - connect(2)

现在变得绝望,尝试了一个基本的 curl 请求:

curl http://mysite.com
curl: (7) couldn't connect to host

显然,我意识到这不再是 ruby​​ 问题,也绝对不是 curl 问题。必须有某种防火墙阻止到服务器所在机器的流量。我可以用 curl 很好地连接到谷歌,但不能连接到那个网站。

4

0 回答 0