Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using uTorrent Web API via .NET

I'm trying to get list of torrents from uTorrent using Web API. Getting required token goes O.K.:

WebClient client = new WebClient() { Credentials = new NetworkCredential(UserName, pass) };
StreamReader Reader = new StreamReader(client.OpenRead("http://localhost:" + port + "/gui/token.html"));
string token = Reader.ReadToEnd();
token = token.Split('>')[2].Split('<')[0]; 
// token is now something like 3LemfrO_-A-SNBXlnQ2QcQWTYydx7qOqKb1W1S54JJW74Ly3EYGgu0xQSU4AAAAA

But when I try to use it to get list of torrents:

Reader = new StreamReader(client.OpenRead("http://localhost:" + port + "/gui/?list=1&token=" + token));

all I get is "Error 400 Bad request".

I've tried to get token manually. In browser page "http://localhost:30303/gui/?list=1&token=3LemfrO_-A-SNBXlnQ2QcQWTYydx7qOqKb1W1S54JJW74Ly3EYGgu0xQSU4AAAAA" opens as it should, but in C# with the same link without any variables I still get error 400. The interesting part is that if switch off token authentication WebClient load page perfectly with and without

"&token=3LemfrO_-A-SNBXlnQ2QcQWTYydx7qOqKb1W1S54JJW74Ly3EYGgu0xQSU4AAAAA"

but token auth enabled by default, so my and any app should use it. And yes, WebRequest/HttpWebRequest didn't help also.

P.S. sorry for my English, I was never able to make it work right

like image 222
MadRunner Avatar asked Feb 23 '23 06:02

MadRunner


1 Answers

you have to save the cookie from the request

Classes.CookieAwareWebClient client = new Classes.CookieAwareWebClient() { Credentials = new NetworkCredential("shehab", "shehab") };
StreamReader Reader = new StreamReader(client.OpenRead("http://localhost:" + "8080" + "/gui/token.html"));
string token = HtmlRemoval.StripTagsRegexCompiled(Reader.ReadToEnd());
MessageBox.Show(token);

Reader = new StreamReader(client.OpenRead("http://localhost:" + "8080" + "/gui/?list=1&token=" + token));
MessageBox.Show(Reader.ReadToEnd());

and for the cookie aware class go to the following link(Using CookieContainer with WebClient class) as web client doesn't support cookies.

like image 182
Shehab Fawzy Avatar answered Mar 03 '23 15:03

Shehab Fawzy