简体   繁体   English

NSURLCredential无法用于iPhone中的syncnuos请求

[英]NSURLCredential not working for synchronuos request in iphone

I am using Synchronous request and passing the credentials but I am getting authentication error in response. 我正在使用同步请求并传递凭据,但是响应中出现身份验证错误。 Below is my code of request and response from server. 以下是我的服务器请求和响应代码。

Request:- 请求:-

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];

/////////// Response ///////////回应

401 Authorization Required 需要401授权


Note: If I am sending the request with async request it is working fine as in that case didReceiveAuthenticationChallenge is giving them the required credentials when asked for. 注意:如果我通过异步请求发送请求,则它工作正常,因为在这种情况下, didReceiveAuthenticationChallenge会在要求时向其提供所需的凭据。

I am sure there might be something missing in my code. 我确定我的代码中可能缺少某些内容。

Waiting for your valuable inputs. 等待您的宝贵意见。

Have you ever tried ASIHTTPRequest which makes easier http operations in IPhone enviroment. 您是否尝试过ASIHTTPRequest ,它可以在iPhone环境中简化 http操作。 And It has really simple and good documentation. 而且它的确非常简单和完善。 I used to use it inside of my projects, It was working awesome. 我曾经在项目内部使用过它,它的工作很棒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM