Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if URL is a torrent in C# .NET

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 :)

like image 855
IQlessThan70 Avatar asked Apr 28 '10 18:04

IQlessThan70


2 Answers

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.

like image 145
bobince Avatar answered Sep 20 '22 07:09

bobince


The only way to know if it's really a torrent file is to download it and check if it's a torrent file.

like image 27
dtb Avatar answered Sep 23 '22 07:09

dtb