gpt4 book ai didi

ruby - 使用 Ruby 转义和下载 URL

转载 作者:数据小太阳 更新时间:2023-10-29 08:06:10 25 4
gpt4 key购买 nike

我尝试从 URL 下载 HTML 内容但没有成功。

这是网址:

http://example.com/some_string[value]

使用 RestClient 时出现此错误:

URI::InvalidURIError: bad URI(is not URI?)

我从 Ruby on Rails IRC 那里得到了一些帮助。想法是转义 URL 的末尾。

$ "http://example.com/" + CGI::escape("some_string[value]")
=> "http://example.com/some_string%5Bvalue%5D"

生成的 URL 无效,我收到 404。不过它在浏览器中有效。

有人知道如何让它工作吗?

最佳答案

根据URI RFC :

Other characters are excluded because gateways and other transport agents are known to sometimes modify such characters, or they are used as delimiters.

unwise = "{" | "}" | "|" | "\" | "^" | "[" | "]" | "`"

Data corresponding to excluded characters must be escaped in order to be properly represented within a URI.

相信浏览器的响应或处理链接的能力是有风险的。他们竭尽所能返回页面,而不是强制执行标准,因此无论页面或 URL 是否正确定义,它们都不是权威来源。

RestClient 的响应可能基于 URI,当我测试使用 URI 解析 URL 时返回相同的错误。

我从未见过使用未编码的“[”和“]”字符的 URL。

关于ruby - 使用 Ruby 转义和下载 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4999322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com