[英]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 有什么特別之處?
不同之處在於,您的url
是String
的一個實例,恰好是 URL。但是因為它只是一個簡單的字符串,所以沒有特殊含義。
而uri
是URI
的一個實例,它不僅是一個簡單的字符串,而且已經分析了字符串中的 URL,它現在提供了幾種優化的方法來返回 URL 的特定部分——比如協議、主機名、路徑或查詢參數.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.