简体   繁体   English

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

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

I have a Ruby code:我有一个 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, '/')

Which produces errors:产生错误:

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

and

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)

But it works perfectly with uri:但它与 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)

So, could anyone explain what is the difference, why it works so, and what is so special about URI?那么,谁能解释一下有什么区别、为什么会这样,以及 URI 有什么特别之处?

The difference is that your url is an instance of String which happens to be a URL. But because it is just a simple string is has no special meaning.不同之处在于,您的urlString的一个实例,恰好是 URL。但是因为它只是一个简单的字符串,所以没有特殊含义。

Whereas uri is an instance of URI which is not only a simple string but had the URL from the string already analyzed and it now offers several optimized methods to return specific parts of the URL – like the protocol, the hostname, the path or query parameters.uriURI的一个实例,它不仅是一个简单的字符串,而且已经分析了字符串中的 URL,它现在提供了几种优化的方法来返回 URL 的特定部分——比如协议、主机名、路径或查询参数.

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

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