我的一台服务器使用net :: http连接到我的其他服务器(在另一台机器上)。当我使用curl时,它可以工作(在localhost上):
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}
在localhost上,一切正常。在我的一台服务器上,完全不同的场景。
当我在我的ubuntu服务器上使用net :: http时,我得到Errno :: ECONNREFUSED:连接被拒绝 - 连接(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 get request:
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 http://mysite.com
curl: (7) couldn't connect to host
显然,我意识到这不再是红宝石问题,绝对不是卷曲问题。必须有某种防火墙阻止服务器所在机器的流量。我可以通过卷曲连接谷歌,但不是那个网站。