如何确定给定的URL链接是否是视频或图像? [英] How to determine if a given URL link is a video or image?

查看:934
本文介绍了如何确定给定的URL链接是否是视频或图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取用户输入的给定网址,并确定该网址是指向图片还是视频。



示例用例:



当用户粘贴YouTube视频的网址时,保存页面将自动显示嵌入式YouTube播放器。



当用户在Flickr上发布图片的URL时,保存时,该页面将自动显示较小版本的Flickr图像。

解决方案

您可以获取URL并从响应中看到Content-type。



您可以使用apache的 HTTP Client ,它可以帮助您获取URL的内容,您可以使用它来浏览重定向。例如,尝试获取以下内容:



http://www.youtube.com/watch?v=d4LkTstvUL4



会返回包含视频的HTML。过了一会儿,你会发现视频在这里:



http://www.youtube.com/v/d4LkTstvUL4



但是如果您抓取该页面,您将得到一个重定向:

  HTTP / 1.0 302重定向
日期:2009年1月23日星期五02:25:37 GMT
Content-Type :text / plain
过期时间:2009年1月23日星期五02:25:37 GMT
Cache-Control:no-cache
服务器:Apache
X-Content-Type-Options :nosniff
Set-Cookie:VISITOR_INFO1_LIVE = sQc75zc-QSU;路径= /;域= .youtube.com; expires =
Set-Cookie:VISITOR_INFO1_LIVE = sQc75zc-QSU;路径= /;域= .youtube.com; expires =
位置:http://www.youtube.com/swf/l.swf?swf=http%3A//s.ytimg.com/yt/swf/cps-vf
L4& rel = 1& eurl =& iurl = http%3A // i1.ytimg.com / vi / d4LkTstvUL4 / hqdefault.jpg& sk = Z_TM3JF
e_get_video_info = 1& load_modules = 1

所以,你需要做的是获取URL并检查它,直到获得最终内容为止。 b

本节说明如何处理重定向。


I'm trying to take a given URL entered by user and determine if the URL is pointing to a image or a video.

Example use case:

When a user paste in the URL of a YouTube video, on save the page will auto display the embedded YouTube player.

When a user posts URL of a picture in Flickr, on save, the page will auto display a smaller version of the Flickr image.

解决方案

You can fetch the URL and see Content-type from the response.

You can use the HTTP Client from apache, it helps you to fetch the content of the URL and you can use it to navigate the redirects. For instance try to fetch the following:

http://www.youtube.com/watch?v=d4LkTstvUL4

Will return an HTML containing the video. After a while you'll find out the video is here:

http://www.youtube.com/v/d4LkTstvUL4

But if you fetch that page you will get a redirect:

HTTP/1.0 302 Redirect
Date: Fri, 23 Jan 2009 02:25:37 GMT
Content-Type: text/plain
Expires: Fri, 23 Jan 2009 02:25:37 GMT
Cache-Control: no-cache
Server: Apache
X-Content-Type-Options: nosniff
Set-Cookie: VISITOR_INFO1_LIVE=sQc75zc-QSU; path=/; domain=.youtube.com; expires=
Set-Cookie: VISITOR_INFO1_LIVE=sQc75zc-QSU; path=/; domain=.youtube.com; expires=
Location: http://www.youtube.com/swf/l.swf?swf=http%3A//s.ytimg.com/yt/swf/cps-vf
L4&rel=1&eurl=&iurl=http%3A//i1.ytimg.com/vi/d4LkTstvUL4/hqdefault.jpg&sk=Z_TM3JF
e_get_video_info=1&load_modules=1

So, what you have to do is to fetch the URL and examine it, until you get final content

This section explains how to handle the redirects.

这篇关于如何确定给定的URL链接是否是视频或图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆