通过 .NET 使用 uTorrent Web API
我正在尝试使用 Web API 从 uTorrent 获取 torrent 列表。获取所需的令牌没问题:
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
但是当我尝试使用它来获取 torrent 列表时:
Reader = new StreamReader(client.OpenRead("http://localhost:" + port + "/gui/?list=1&token=" + token));
我得到的只是“错误 400 错误请求”。
我尝试过手动获取令牌。在浏览器页面“http://localhost:30303/gui/?list=1&token=3LemfrO_-A-SNBXlnQ2QcQWTYydx7qOqKb1W1S54JJW74Ly3EYGgu0xQSU4AAAAA”中,它应该按预期打开,但在没有任何变量的相同链接的 C# 中,我仍然收到错误 400。 有趣的部分是,如果关闭令牌身份验证 WebClient 可以完美地加载页面,无论是否启用
"&token=3LemfrO_-A-SNBXlnQ2QcQWTYydx7qOqKb1W1S54JJW74Ly3EYGgu0xQSU4AAAAA"
令牌身份验证,但默认启用令牌身份验证,因此我的和任何应用程序都应该使用它。 是的,WebRequest/HttpWebRequest 也没有帮助。
PS,对不起我的英语,我从来没能让它正常工作
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须保存请求中的 cookie
,对于 cookie 感知类,请转到以下链接(将 CookieContainer 与 WebClient 类一起使用)作为 Web 客户端不支持 cookie。
you have to save the cookie from the request
and for the cookie aware class go to the following link(Using CookieContainer with WebClient class) as web client doesn't support cookies.
您应该保存请求中的 cookie
,然后在使用相同令牌时在 uTorrent 的每个其他请求中使用它:
You should save cookies from request
And then use it in every other request to uTorrent when using the same token:
我有一个简单的三步建议:
当您使用带有令牌的浏览器时,请使用Fiddler2 分析服务器和浏览器之间的 HTTP 流量。
打开您的 C# 应用程序并使用 Fiddler2 分析服务器与您的应用程序之间的 HTTP 流量。
将浏览器的 HTTP 请求和响应与 C# 应用程序的请求和响应进行比较。如果您发现显着差异,则很可能存在问题。
I have a simple 3-step suggestion:
When you use your browser with the token, use Fiddler2 to analyze the HTTP traffic between the server and browser.
Open up your C# app and use Fiddler2 to analyze the HTTP traffic between the server and your app.
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.