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

有帮助吗?

解决方案

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.

其他提示

You should save cookies from request

WebRequest request = WebRequest.Create("http://localhost:" + port + "/gui/token.html");
CookieContainer cookies = new CookieContainer();
(request as HttpWebRequest).CookieContainer = cookies;

And then use it in every other request to uTorrent when using the same token:

request = WebRequest.Create("http://localhost:" + port + "/gui/?list=1&token=" + token);
(request as HttpWebRequest).CookieContainer = cookies;

I have a simple 3-step suggestion:

  1. When you use your browser with the token, use Fiddler2 to analyze the HTTP traffic between the server and browser.

  2. Open up your C# app and use Fiddler2 to analyze the HTTP traffic between the server and your app.

  3. Compare the HTTP requests and responses for the browser with the requests and responses for the C# app. If you see a significant difference, there is a good chance that could be the problem.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top