utorrent webui を使用して自分のコンピューターにログインしようとしています。
現在、次のコードを使用しています。
- (void)login
{
NSURL *feedsURL = [NSURL URLWithString:@"http://localhost:12345/"];
NSString *user = @"username";
NSString *password = @"password";
NSURLCredential *defaultCredential = [NSURLCredential credentialWithUser:user password:password persistence:NSURLCredentialPersistencePermanent];
NSString *host = [feedsURL host];
NSInteger port = [[feedsURL port] integerValue];
NSString *protocol = [feedsURL scheme];
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc] initWithHost:host port:port protocol:protocol realm:nil authenticationMethod:NSURLAuthenticationMethodHTTPBasic];
NSURLCredentialStorage *credentials = [NSURLCredentialStorage sharedCredentialStorage];
[credentials setDefaultCredential:defaultCredential forProtectionSpace:protectionSpace];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
[config setURLCredentialStorage:credentials];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:nil delegateQueue:nil];
[[session dataTaskWithURL:feedsURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
if (httpResp.statusCode == 200) {
NSLog(@"Woo! everything is working");
} else {
NSLog(@"Hrmmm... error %@ occured", error);
}
} else {
NSLog(@"Hrmmm... error %@ occured", error);
}
}] resume];
デスクトップ Web ブラウザーを使用している間は問題なくページを表示できますが、私のアプリでは常に 401 エラー (承認されていません) が返されます。
これは、資格情報が使用されていないためだと思いますか? もしそうなら、それを使用する正しい方法は何ですか?また、NSURLSessionDelegate メソッドは呼び出されていません。他にやらなければならないことはありますか?