检查 URL 是否为 C# .NET 中的 torrent
对我来说,获取网址的最佳方式是什么: http://foobar.com/foo.torrent并检查这是否真的是一个 torrent,而不是一个 html 页面或其他有趣的东西。 建议? 谢谢 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
要检查资源的类型而不下载资源,请使用 HEAD 请求:
但是,某些服务器中可能未设置
application/x-bittorrent
类型,因此您可能会得到而是使用 application/octet-stream
,如果运气不好的话甚至可以使用text/plain
。如果您需要考虑到这一点,那么您所能做的就是使用普通的 GET 请求来获取文件,并查看是否可以对其进行解码。BitTorrent 文件格式基于一种名为“bencode”的格式。 这里是一个声称可以处理该问题的 .NET 库。您可以猜测任何有效的 bencode 文件都是 torrent 文件,但如果您想确保可以查看它解码到的映射并检查
info
和announce
特性。To check the type of a resource without downloading it, use a HEAD request:
However, the type
application/x-bittorrent
might not be set up in some servers, so it's possible you might getapplication/octet-stream
instead, or eventext/plain
if you are unlucky. If you need to account for this, about all you could do would be to fetch the file with a normal GET request, and see if you can decode it.The BitTorrent file format is based around a format called ‘bencode’. Here's a .NET library that claims to handle it. You can guess any file that's valid bencode is a torrent file, but if you want to make sure you can look at the mapping it decodes to and check for the
info
andannounce
properties.知道它是否真的是 torrent 文件的唯一方法是下载它并检查它是否是 torrent 文件。
The only way to know if it's really a torrent file is to download it and check if it's a torrent file.
可能最好下载并验证它和/或检查内容类型是
application/x-bittorrent
。Probably best to download and validate it and/ or check the content type is
application/x-bittorrent
.阅读 torrent 文件规范,然后编写一个 C# 应用程序来下载 URL 的内容,并查看它是否符合规范中的规则。
Read the torrent file specification, then write a C# app to download the contents of the URL and see if it meets the rules in the specification.
除了 bobince 提供的好答案之外,您还可以查看 monotorrent开源 C# 实现。他们下载整个 .torrent 文件并随后解析 Bencode(参见:http://anonsvn.mono-project.com/viewvc/trunk/bitsharp/src/MonoTorrent/MonoTorrent.Common/Torrent.cs 第 611 行)
In additon to the good answer bobince provided, you could also have a look at the monotorrent open source c# implementation. They download the whole .torrent file and parse the bencode afterwards (cf.: http://anonsvn.mono-project.com/viewvc/trunk/bitsharp/src/MonoTorrent/MonoTorrent.Common/Torrent.cs lines 611ff)
我会使用 MonoTorrent 库。具体来说,您可以使用静态方法
,该方法将返回一个布尔值,指示
url
参数是否指向有效的 torrent 文件。I would use the MonoTorrent library. Specifically, you could use the static method
which will return a boolean value indicating whether the
url
parameter points to a valid torrent file.如果您愿意学习一些 C++,您可以使用 P/Invoke 对 libtorrent 库进行外部调用我确信它有一种验证文件的方法。
If you are willing to learn some c++ you could make a external call to the libtorrent library with a P/Invoke I am shure it has a way to validate files.