【问题标题】:Ruby net/http/get does not work with url's, but works with uri's. Why?Ruby net/http/get 不适用于 url,但适用于 uri。为什么?
【发布时间】:2021-05-03 06:17:27
【问题描述】:

我有一个 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 有什么特别之处?

【问题讨论】:

  • Documentation 提到The target can either be specified as (uri), or as (host, path, port = 80) 所以第一次调用成功应该是Net::HTTP.get_response('stackoverflow.com', '/questions/65798852/how-do-i-set-an-electron-variable-at-compile-time')

标签: ruby http get


【解决方案1】:

不同之处在于您的urlString 的一个实例,它恰好是一个URL。但是因为它只是一个简单的字符串所以没有什么特别的意义。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-16
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    相关资源
    最近更新 更多