Я пытаюсь войти на свой компьютер, используя 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];
Я отлично вижу страницу при использовании настольного веб-браузера, однако в моем приложении всегда возвращается ошибка 401 (не авторизовано).
Я предполагаю, что это потому, что учетные данные не используются? Если да, то как правильно его использовать? Также не вызываются методы NSURLSessionDelegate. Есть ли что-то еще, что я должен сделать?