1

我一直试图让这段代码能够将种子数据发送到 µTorrent Web API,不幸的是,据我所知,以前没有人尝试过在 Objective-C 中为 µTorrent 制作 .torrent 文件处理程序,这这并不奇怪。

我现在有这段代码,但我总是收到错误消息:“错误:表单参数中未提供 torrent 文件内容”。

NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?token=%@&action=add-file", [self getBaseURL], token]]];
NSString * boundary = @"!@!@!@!@!@!@!@!@!";
NSMutableData * body = [NSMutableData alloc];
NSData * torrentFileContents = [[NSFileManager defaultManager] contentsAtPath:fileName];

[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"enctype"];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", @"application/x-bittorrent"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"torrent_file\"; filename=\"%@\"\r\n", fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Length: %ld\r\n", [torrentFileContents length]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:torrentFileContents]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];


[request setHTTPBody:body];
4

2 回答 2

1

为什么将内容类型和长度放在请求正文中?您是否尝试使用 setValue 添加必要的 HTTP 标头,例如 Content-Type?

例如:

[request addValue:@"text/xml;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
于 2013-03-28T03:14:03.007 回答
1

我找到了一个我想做的例子,用 JS 编写,我可以通过调整代码让它看起来像这样。

NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?token=%@&action=add-file", [self getBaseURL], token]]];
NSString * boundary = [NSString stringWithFormat:@"AJAX-----------------------%f", [[[NSDate alloc] init] timeIntervalSince1970]];
NSMutableData * body = [NSMutableData alloc];
NSData * torrentFileContents = [[NSFileManager defaultManager] contentsAtPath:fileName];

[request setHTTPMethod:@"POST"];
[request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"torrent_file\"; filename=\"%@\"\r\n", fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", @"application/x-bittorrent"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:torrentFileContents]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];

return request;
于 2013-03-30T08:32:00.450 回答