Ruby net/http/get 不适用于 url,但适用于 uri。为什么?



我有一个Ruby代码

require 'net/http'
url = "https://stackoverflow.com/questions/65798852/how-do-i-set-an-electron-variable-at-compile-time"
response = Net::HTTP.get_response(url, '/')

产生错误:

getaddrinfo: nodename nor servname provided, or not known (SocketError)

Failed to open TCP connection to https://stackoverflow.com/questions/65798852/how-do-i-set-an-electron-variable-at-compile-time:80 (getaddrinfo: nodename nor servname provided, or not known) (SocketError)

但是它可以完美地与uri:

配合使用。
require 'net/http'
url = "https://stackoverflow.com/questions/65798852/how-do-i-set-an-electron-variable-at-compile-time"
uri = URI(url)
response = Net::HTTP.get_response(uri)

那么,有没有人能解释一下它们之间的区别,为什么会这样,URI有什么特别之处?

不同的是,您的urlString的一个实例,这恰好是一个URL。但是因为它只是一个简单的字符串,所以没有特殊的含义。

uriURI的一个实例,它不仅是一个简单的字符串,而且已经从字符串中分析了URL,现在它提供了几个优化的方法来返回URL的特定部分-如协议,主机名,路径或查询参数。

最新更新