如何修复不良 URI 不是 URI?

62

我正在使用ruby版本1.9.3,我想从以下视频网址中获取主机名:

我尝试了如下代码:

require 'uri'
url = "https://ferrari-view.4me.it/view-share/playerp/?plContext=http://ferrari-%201363948628-stream.4mecloud.it/live/ferrari/ngrp:livegenita/manifest.f4m&cartellaConfig=http://ferrari-4me.weebo.it/static/player/config/&cartellaLingua=http://ferrari-4me.weebo.it/static/player/config/&poster=http://pusher.newvision.it:8080/resources/img1.jpg&urlSkin=http://ferrari-4me.weebo.it/static/player/swf/skin.swf?a=1363014732171&method=GET&target_url=http://ferrari-4me.weebo.it/static/player/swf/player.swf&userLanguage=IT&styleTextColor=#000000&autoPlay=true&bufferTime=2&isLive=true&highlightColor=#eb2323&gaTrackerList=UA-23603234-4"  
puts URI.parse(url).host  

它抛出了异常URI::InvalidURIError: bad URI(is not URI?):

我尝试将URL编码,然后像下面这样解析:

puts URI.parse(URI.parse(url)).host

它抛出了一个相同的异常URI::InvalidURIError: bad URI(is not URI?)

但是上述代码适用于以下URL。

url = http://www.youtube.com/v/GpQDa3PUAbU?version=3&autohide=1&autoplay=1

如何修复这个问题?请提出任何建议。 谢谢


4
URI::InvalidURIError: 坏的URI(不是URI) - viral
在使用代理的情况下,这个链接可能会有所帮助 https://dev59.com/hW435IYBdhLWcg3whAWx#51427832 - Vladimir Vlasov
6个回答

121

这个URL是无效的,但它可以在浏览器中正常工作,因为浏览器本身对像“:”、“/”等特殊字符不太严格。

您应该先对URI进行编码。

encoded_url = URI.encode(url)

然后解析它

URI.parse(encoded_url)

1
你真的节省了我的时间。 在我的情况下,我遇到了 Net::HTTPBadResponse - wrong status line: "{": 的问题,同样的解决方案也适用于它。 - Taimoor Changaiz
当我尝试这样做时,我遇到了NoMethodError: undefinded method 'gsub' for nil:NilClass。然而,当我手动运行Gem时,代码可以正常工作。 - FilBot3
我也看到了undefinded method 'gsub' for nil:NilClass的报错,但事实证明当我没有预料到的时候我的原始url为空。 - Andrew
@KonradSzczęśniak 尝试使用引号中的实际链接,stackoverflow 将字面字符串 example 转换为那个格式。 - Laser
显示剩余2条评论

21

Addressable::URI是URI的更好、更符合rfc标准的替代品:

require "addressable/uri"
Addressable::URI.parse(url).host
#=> "ferrari-view.4me.it"

首先运行gem install addressable命令。


3
我认为这个回答更准确,因为Ruby的URI.encode无法处理某些URI。 - Daniel Cukier
Addressable::URI.parse("https://www. with_space.com").host 不起作用,它会引发 Addressable::URI::InvalidURIError。 - astropanic
Steam使用像https://steamcommunity.com/profiles/[U:1:123456789]这样的URL,而URI.encode无法解决问题,但是Addressable可以。 - WojciechKo

3

试试这个:

safeurl = URI.encode(url.strip)
response = RestClient.get(safeurl)

4
你能详细说明一下这是如何解决问题的吗?谢谢! - DanM7

0
uri = URI.parse(URI.encode(url.strip))

我认为这是错误的,因为它返回相同的链接。 - ramzieus

0

URI.parse 是正确的:该 URI 是非法的。仅仅因为它在您的浏览器中意外地运行,这并不意味着它是合法的。您无法解析该URI,因为它根本不是一个 URI。


3
依据哪个RFC? - pguardiario

0

您的URI查询无效。有几个字符需要使用URI :: encode()进行编码。例如,#&在查询中无效。

以下是您代码的可工作版本

    require 'uri'

    plContext = URI::encode("http://ferrari-%201363948628-stream.4mecloud.it/live/ferrari/ngrp:livegenita/manifest.f4m")
    cartellaConfig = URI::encode("http://ferrari-4me.weebo.it/static/player/config/")
    cartellaLingua = URI::encode("http://ferrari-4me.weebo.it/static/player/config/")
    poster = URI::encode("http://pusher.newvision.it:8080/resources/img1.jpg")
    urlSkin = URI::encode("http://ferrari-4me.weebo.it/static/player/swf/skin.swf?a=1363014732171")
    target_url = URI::encode("http://ferrari-4me.weebo.it/static/player/swf/player.swf")
    url = "https://ferrari-view.4me.it/view-share/playerp/?"
    url << "plContext=#{plContext}"
    url << "&cartellaConfig=#{cartellaConfig}"
    url << "&cartellaLingua=#{cartellaLingua}"
    url << "&poster=#{poster}"
    url << "&urlSkin=#{urlSkin}"
    url << "&method=GET"
    url << "&target_url=#{target_url}"
    url << "&userLanguage=IT"
    url << "&styleTextColor=#{URI::encode("#000000")}"
    url << "&autoPlay=true&bufferTime=2&isLive=true&gaTrackerList=UA-23603234-4"
    url << "&highlightColor=#{URI::encode("#eb2323")}"  
    puts url
    puts URI.parse(url).host

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接