Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 使用NSURLConnection强制身份验证比使用委托方法更快/更好吗?_Objective C_Ios_Authentication_Nsurlconnection - Fatal编程技术网

Objective c 使用NSURLConnection强制身份验证比使用委托方法更快/更好吗?

Objective c 使用NSURLConnection强制身份验证比使用委托方法更快/更好吗?,objective-c,ios,authentication,nsurlconnection,Objective C,Ios,Authentication,Nsurlconnection,如果我知道我的服务器API需要身份验证,直接使用http头强制进行身份验证是否更快/更好,而不是等待服务器返回401响应,然后在NSURLConnection委托方法connection:didReceiveAuthenticationChallenge:中对其进行响应?向服务器提供身份验证凭据的更简单方法是使用NSURLConnection委托方法 didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challen

如果我知道我的服务器API需要身份验证,直接使用http头强制进行身份验证是否更快/更好,而不是等待服务器返回401响应,然后在NSURLConnection委托方法connection:didReceiveAuthenticationChallenge:中对其进行响应?

向服务器提供身份验证凭据的更简单方法是使用NSURLConnection委托方法

didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
您可以在其中提供与此类似的凭据

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

    if ([challenge previousFailureCount] == 0) {

        NSURLCredential *newCredential;
        newCredential = [NSURLCredential credentialWithUser:userName password:password persistence:NSURLCredentialPersistenceNone];
        [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
    } else {
        [[challenge sender] cancelAuthenticationChallenge:challenge];
    }
}
将发生的情况是,您首先使用GET/POST请求调用服务器,如果服务器需要身份验证,并且HTTTP头中没有提供凭据,那么它很可能会使用401响应进行响应。上述方法将触发并提供提供的凭据

但是,如果您知道服务器总是需要身份验证,那么进行这一轮额外的客户机/服务器通信是没有效率的,您最好直接在HTTP头中提供凭据

在HTTP头中提供凭据的方法很简单,只是iOS没有编码到BASE64的方法

NSMutableURLRequest *aRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30];

// first create a plaintext string in the format username:password  
NSMutableString *loginString = (NSMutableString *)[@"" stringByAppendingFormat:@"%@:%@", userName, password];  

// encode loginString to Base64
// the Base64 class is not provided and you will have to write it!
NSString *encodedLoginData = [Base64 encode:[loginString dataUsingEncoding:NSUTF8StringEncoding]];  

// prepare the header value   
NSString *authHeader = [@"Basic " stringByAppendingFormat:@"%@", encodedLoginData];  

// add the authentication credential into the HTTP header
[request addValue:authHeader forHTTPHeaderField:@"Authorization"];  

// provide additional HTTP header properties (optional)     
[aRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[aRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[aRequest setHTTPMethod:@"GET"];

// and finally create your connection for above request
NSURLConnection *aConnection = [[NSURLConnection alloc] initWithRequest:aRequest delegate:self];

// don't forget to release the request and nsurlconnection when appropriate...

向服务器提供身份验证凭据的更简单方法是使用NSURLConnection委托方法

didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
您可以在其中提供与此类似的凭据

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

    if ([challenge previousFailureCount] == 0) {

        NSURLCredential *newCredential;
        newCredential = [NSURLCredential credentialWithUser:userName password:password persistence:NSURLCredentialPersistenceNone];
        [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
    } else {
        [[challenge sender] cancelAuthenticationChallenge:challenge];
    }
}
将发生的情况是,您首先使用GET/POST请求调用服务器,如果服务器需要身份验证,并且HTTTP头中没有提供凭据,那么它很可能会使用401响应进行响应。上述方法将触发并提供提供的凭据

但是,如果您知道服务器总是需要身份验证,那么进行这一轮额外的客户机/服务器通信是没有效率的,您最好直接在HTTP头中提供凭据

在HTTP头中提供凭据的方法很简单,只是iOS没有编码到BASE64的方法

NSMutableURLRequest *aRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30];

// first create a plaintext string in the format username:password  
NSMutableString *loginString = (NSMutableString *)[@"" stringByAppendingFormat:@"%@:%@", userName, password];  

// encode loginString to Base64
// the Base64 class is not provided and you will have to write it!
NSString *encodedLoginData = [Base64 encode:[loginString dataUsingEncoding:NSUTF8StringEncoding]];  

// prepare the header value   
NSString *authHeader = [@"Basic " stringByAppendingFormat:@"%@", encodedLoginData];  

// add the authentication credential into the HTTP header
[request addValue:authHeader forHTTPHeaderField:@"Authorization"];  

// provide additional HTTP header properties (optional)     
[aRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[aRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[aRequest setHTTPMethod:@"GET"];

// and finally create your connection for above request
NSURLConnection *aConnection = [[NSURLConnection alloc] initWithRequest:aRequest delegate:self];

// don't forget to release the request and nsurlconnection when appropriate...

Thilo,您的回答是基于我在这里猜测的客户机和服务器之间的通信较少吗?您可能需要添加您的-注释-作为答案。是的,将减少一次服务器往返。但是如果没有代码示例,在iOS中如何做到这一点,我觉得它只不过是一个注释-Thilo,您的回答是基于我在这里猜测的客户机和服务器之间的通信较少吗?您可能需要添加您的-注释-作为答案。是的,将减少一次服务器往返。但是如果没有代码示例,在iOS中如何做到这一点,我觉得它只不过是一个注释-嗨@peter pajchl,谢谢你的解释。这对我有用。但我仍然有一个问题,从服务器加载图像,我认为是由于相同的身份验证问题与下面的代码行。[myImageView setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[dic objectForKey:@image][]]];你能帮帮我吗in@SudhanshuSrivastava从代码片段中很难找出问题所在。我建议您创建一个新问题,并提供更多信息以及您得到的http响应。但是您应该注意的是…在您的代码中,我看不到您在哪里准备请求和设置凭据。您好@peter pajchl,谢谢您的解释。这对我有用。但我仍然有一个问题,从服务器加载图像,我认为是由于相同的身份验证问题与下面的代码行。[myImageView setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[dic objectForKey:@image][]]];你能帮帮我吗in@SudhanshuSrivastava从代码片段中很难找出问题所在。我建议您创建一个新问题,并提供更多信息以及您得到的http响应。但您应该注意的是……在您的代码中,我看不到您在哪里准备请求和设置凭据。