繁体   English   中英

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

[英]Ruby net/http/get does not work with url's, but works with uri's. Why?

我有一个 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 的特定部分——比如协议、主机名、路径或查询参数.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM