如何修复错误的 URI 不是 URI [英] How to fix bad URI is not URI
问题描述
我使用的是 ruby 版本 1.9.3
,我喜欢从下面的视频 url 中获取主机名,
I'm using the ruby version 1.9.3
, I like to get host name from the video url below,
我尝试过代码
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?):
it throws an exception URI::InvalidURIError: bad URI(is not URI?):
我尝试对 URL 进行编码,然后像下面这样解析
I tried with encode the URL then parse like below
puts URI.parse(URI.parse(url)).host
它抛出一个异常 URI::InvalidURIError: bad URI(is not URI?)
但以上代码适用于以下网址.
But above code works for the below URL.
url = http://www.youtube.com/v/GpQDa3PUAbU?version=3&autohide=1&autoplay=1
如何解决这个问题?请提出任何建议.谢谢
How to fix this? any suggestion please. Thanks
推荐答案
此 url 无效,但它在浏览器中有效,因为浏览器本身对 :、/等特殊字符不那么严格
This url is not valid, but it works in browser because browser itself is less strict about special characters like :, /, etc.
您应该先对您的 URI 进行编码
You should encode your URI first
encoded_url = URI.encode(url)
然后解析它
URI.parse(encoded_url)
这篇关于如何修复错误的 URI 不是 URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!