如何修复错误的 URI 不是 URI

编程入门 行业动态 更新时间:2024-10-26 04:24:43
本文介绍了如何修复错误的 URI 不是 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 = "ferrari-view.4me.it/view-share/playerp/?plContext=ferrari-%201363948628-stream.4mecloud.it/live/ferrari/ngrp:livegenita/manifest.f4m&cartellaConfig=ferrari-4me.weebo.it/static/player/config/&cartellaLingua=ferrari-4me.weebo.it/static/player/config/&poster=pusher.newvision.it:8080/resources/img1.jpg&urlSkin=ferrari-4me.weebo.it/static/player/swf/skin.swf?a=1363014732171&method=GET&target_url=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 = www.youtube/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

本文发布于:2023-11-10 03:13:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574195.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   URI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!