gpt4 book ai didi

Ruby:以困难的方式扩展缩短网址

转载 作者:数据小太阳 更新时间:2023-10-29 07:29:12 25 4
gpt4 key购买 nike

有没有办法在 ruby​​ 中打开 URLS 并输出重定向的 url:即转换http://bit.ly/l223uehttp://paper.li/CoyDavidsonCRE/1309121465

我发现 url 缩短服务比 gem 跟上的要多,所以我要求使用困难但稳健的方法,而不是使用连接到某些 API 的 gem。

最佳答案

这是一个加长的方法

这几乎没有错误处理,但它可能会帮助您入门。您可以使用返回 nil 的 begin rescue block 包装 lengthen 或尝试稍后重试。不确定您要构建什么,但希望对您有所帮助。

require 'uri'
require 'net/http'

def lengthen(url)
uri = URI(url)
Net::HTTP.new(uri.host, uri.port).get(uri.path).header['location']
end


irb(main):008:0> lengthen('http://bit.ly/l223ue')
=> "http://paper.li/CoyDavidsonCRE/1309121465"

关于Ruby:以困难的方式扩展缩短网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9306275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com