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それをエスケープするだけで、他には何もありません。URIに渡すには、実際の a のインスタンスが必要ですget_response

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