3

我试图简单地用空格转义一个 URL,然后在 Ruby 中对该 URL 执行 GET 请求。

我的错误是

/Users/user/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:393:in `get_response': undefined method `host' for "http://google.com/?this%20is%20a%20stromg%20with%20spaces":String (NoMethodError)
from test_url.rb:6:in `<main>'

这是当前代码

require 'rubygems'
require 'net/http'

uri = URI.escape("http://google.com/?this is a string with spaces")
res = Net::HTTP.get_response(uri)
puts res.body if res.is_a?(Net::HTTPSuccess) 

Net::HTTP.start(uri.host, uri.port) do |http|
  request = Net::HTTP::Get.new uri.request_uri

  response = http.request request # Net::HTTPResponse object
end
4

1 回答 1

6

URI.escape只是逃避它,没有别的。您需要 a 的实际实例URI传递给get_response

uri = URI.parse(URI.escape("http://google.com/?this is a string with spaces"))
于 2012-05-26T12:14:08.657 回答