NSURLCredential不适用于iphone中的同步请求

时间:2010-06-14 06:34:08

标签: iphone-sdk-3.0 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 在被要求时为他们提供所需的凭据。

我确信我的代码中可能缺少某些内容。

等待您的宝贵意见。

1 个答案:

答案 0 :(得分:0)

你有没有试过ASIHTTPRequest,这使得在IPhone环境中更容易进行http操作。它有非常简单和好的文档。我以前在我的项目中使用它,它工作得很棒。