4

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 メソッドは呼び出されていません。他にやらなければならないことはありますか?

4

2 に答える 2

1

HTTP Basic は、通常、保護スペースにレルムを必要とします。これはおそらく失敗している場所です。を使用してサーバーから返された認証チャレンジ ヘッダーをチェックしてcurl -v、それが何であるかを確認します。

それ以外は、リストしたコードが機能するはずです-非常によく似たものを実装したところ、資格情報が正しく渡されています。また、代理認証チャレンジ メソッドが呼び出されないという問題も経験しました。

于 2013-11-06T15:30:32.230 に答える
0

localhost をコンピュータの IP アドレスとポートに置き換えてみてください

于 2013-12-24T19:38:42.770 に答える