繁体   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