如何修复坏URI不是URI [duplicate]

时间:2022-03-12 13:08:40

This question already has an answer here:

这个问题已经有了答案:

I'm using the ruby version 1.9.3, I like to get host name from the video url below,

我使用的是ruby版本1.9.3,我喜欢从下面的视频url中获取主机名,

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(不是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?)

它抛出一个异常:::InvalidURIError: bad URI(不是URI吗?)

But above code works for the below URL.

但是上面的代码适用于下面的URL。

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

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

How to fix this? any suggestion please. Thanks

如何解决这个问题?任何建议请。谢谢

6 个解决方案

#1


91  

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)

#2


15  

Addressable::URI is a better, more rfc-compliant replacement for URI:

可寻址:URI是更好的、更符合rpc的URI替换:

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

gem install addressable first.

gem安装可寻址。

#3


1  

try this:

试试这个:

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

#4


0  

Your URI query is not valid. There are several characters that you should encode with URI::encode(). For instance, #, , or & are not valid in a query.

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

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

#5


0  

URI.parse is 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't a URI.

URI。解析是正确的:该URI是非法的。仅仅因为它碰巧在你的浏览器中工作并不意味着它是合法的。无法解析该URI,因为它不是URI。

#6


0  

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

#1


91  

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)

#2


15  

Addressable::URI is a better, more rfc-compliant replacement for URI:

可寻址:URI是更好的、更符合rpc的URI替换:

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

gem install addressable first.

gem安装可寻址。

#3


1  

try this:

试试这个:

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

#4


0  

Your URI query is not valid. There are several characters that you should encode with URI::encode(). For instance, #, , or & are not valid in a query.

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

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

#5


0  

URI.parse is 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't a URI.

URI。解析是正确的:该URI是非法的。仅仅因为它碰巧在你的浏览器中工作并不意味着它是合法的。无法解析该URI,因为它不是URI。

#6


0  

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