簡體   English   中英

NSURLCredential無法用於iPhone中的syncnuos請求

[英]NSURLCredential not working for synchronuos request in iphone

我正在使用同步請求並傳遞憑據,但是響應中出現身份驗證錯誤。 以下是我的服務器請求和響應代碼。

請求:-

NSURLCredential *userCredentials = [NSURLCredential  credentialWithUser:@"username"
                                                               password:@"paswd"
                                                            persistence:NSURLCredentialPersistenceForSession];

NSURLProtectionSpace *space = [[NSURLProtectionSpace alloc] initWithHost:@"http://webaddress.inc.com"
                                                                    port:80
                                                                protocol:@"http"
                                                                   realm:@"webaddress.inc.com"
                                                    authenticationMethod:nil];

[[NSURLCredentialStorage sharedCredentialStorage] setCredential:userCredentials
                                             forProtectionSpace:space];

NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://webaddress.inc.com"]
                                                          cachePolicy:NSURLCacheStorageNotAllowed
                                                      timeoutInterval:30];


NSURLResponse *response;
NSError *error;

NSData *returnData = [NSURLConnection sendSynchronousRequest:urlRequest
                                           returningResponse:&response
                                                       error:&error];

///////////回應

需要401授權


注意:如果我通過異步請求發送請求,則它工作正常,因為在這種情況下, didReceiveAuthenticationChallenge會在要求時向其提供所需的憑據。

我確定我的代碼中可能缺少某些內容。

等待您的寶貴意見。

您是否嘗試過ASIHTTPRequest ,它可以在iPhone環境中簡化 http操作。 而且它的確非常簡單和完善。 我曾經在項目內部使用過它,它的工作很棒。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM