检查网址指向一个文件或页面 [英] Check if url leads to a file or a page

查看:121
本文介绍了检查网址指向一个文件或页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要以下条件:

从URL下载一个文件,如果它实际上是一个文件。 。否则,如果它是一个网页无能为力

Download a file from an URL if it is actually a file. Otherwise if its a page do nothing.

有关一个简单的例子,我得到了下面的下载文件:

For a quick example I've got the following to download a file:

 My.Computer.Network.DownloadFile(
"http://www.wired.com/wiredenterprise/wp-content/uploads/2013/07/ff_googleinfrastructure_large.jpg",
"d:\ff_googleinfrastructure_large.jpg")

但是,如果我们得到了一个正常的网页,如 http://www.google.com 它只是下载这是我们不希望的页面。

But if we got a normal web page for example "http://www.google.com" it will just download the page which is something we do not want.

所以,我怎么能找出如果一个URL将导致一个文件,而不是网页?

So how can i find out if an URL will lead to a file instead of a page?

它可以是任何类型的文件,以便检查是否URL以.zip或.jpg或.DOCX或...根本不会做结束。

It could be any type of file so checking if the URL ends with .zip or .jpg or .docx or... simply won't do.

答案都欢迎这就是为什么我都标记

Answers in VB.NET or C# are both welcome which is why i marked both.

推荐答案

的时间提前,有没有100%准确的方法。你可以检查扩展(假如有),但即使不是100%万无一失。

Ahead of time, there's no 100% accurate way. You could check the extension (assuming there is one), but even that is not 100% foolproof.

您可以发出请求并检查内容类型页眉和救助下载如果值的文件为的text / html 或者一些文本 MIME变种。由于olydis低于所指出的,可以执行 HEAD 请求只得到响应头回来,如果你想下载完整的文件在这一点上再决定。

You could make the request and examine the content-type header and bail out of downloading the file if the value is text/html or some text MIME variant. As olydis points out below, you can perform a HEAD request to just get the response header back and decide then if you want to download the file in its entirety at that point.

这篇关于检查网址指向一个文件或页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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