簡體   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