ruby 如何修复错误的 URI 不是 URI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15700784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fix bad URI is not URI
提问by prabu
I'm using the ruby version 1.9.3, I like to get host name from the video url below,
我正在使用 ruby 版本1.9.3,我喜欢从下面的视频网址中获取主机名,
I tried with code
我试过代码
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
it throws an exception URI::InvalidURIError: bad URI(is not URI?):
它抛出一个异常 URI::InvalidURIError: bad URI(is not URI?):
I tried with encode the URL then parse like below
我尝试对 URL 进行编码,然后像下面这样解析
puts URI.parse(URI.parse(url)).host
it throws an exception same URI::InvalidURIError: bad URI(is not URI?)
它抛出一个相同的异常 URI::InvalidURIError: bad URI(is not URI?)
But above code works for the below URL.
但上面的代码适用于下面的 URL。
url = http://www.youtube.com/v/GpQDa3PUAbU?version=3&autohide=1&autoplay=1
网址 = http://www.youtube.com/v/GpQDa3PUAbU?version=3&autohide=1&autoplay=1
How to fix this? any suggestion please. Thanks
如何解决这个问题?请提出任何建议。谢谢
回答by Konrad Szcz??niak
This url is not valid, but it works in browser because browser itself is less strict about special characters like :, /, etc.
这个 url 无效,但它在浏览器中有效,因为浏览器本身对特殊字符(如 :、/ 等)不太严格。
You should encode your URI first
您应该先对您的 URI 进行编码
encoded_url = URI.encode(url)
And then parse it
然后解析它
URI.parse(encoded_url)
回答by pguardiario
Addressable::URI is a better, more rfc-compliant replacement for URI:
Addressable::URI 是 URI 的更好、更符合 rfc 的替代品:
require "addressable/uri"
Addressable::URI.parse(url).host
#=> "ferrari-view.4me.it"
gem install addressablefirst.
gem install addressable第一的。
回答by angela
try this:
尝试这个:
safeurl = URI.encode(url.strip)
response = RestClient.get(safeurl)
回答by rusllonrails
uri = URI.parse(URI.encode(url.strip))
回答by toch
Your URI query is not valid. There are several charactersthat you should encode with URI::encode(). For instance, #, , or &are not valid in a query.
您的 URI 查询无效。有几个字符应该用URI::encode(). 例如,#、、 或&在查询中无效。
Below a working version of your code
在您的代码的工作版本下方
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
回答by J?rg W Mittag
URI.parseis right: that URI is illegal. Just because it accidentally happens to work in your browser doesn't make it legal. You cannot parse that URI, because it isn'ta URI.
URI.parse是对的:那个 URI 是非法的。仅仅因为它意外地在您的浏览器中工作并不能使其合法。您无法解析该 URI,因为它不是URI。

