What's the best way for me to take url like: http://foobar.com/foo.torrent and check if that really is a torrent, not a html page or something else funny. Suggestions? Thank you :)
To check the type of a resource without downloading it, use a HEAD request:
WebRequest request= WebRequest.Create("http://foobar.com/foo.torrent");
request.Method= "HEAD";
WebResponse response= request.GetResponse();
if (response.Headers.Get("Content-Type")=="application/x-bittorrent") {
...
However, the type application/x-bittorrent
might not be set up in some servers, so it's possible you might get application/octet-stream
instead, or even text/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
and announce
properties.
The only way to know if it's really a torrent file is to download it and check if it's a torrent file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With