我试图简单地使用空格转义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
答案 0 :(得分:6)
URI.escape
只是逃避它,没有别的。您需要将URI
的实际实例传递给get_response
:
uri = URI.parse(URI.escape("http://google.com/?this is a string with spaces"))