0

我正在尝试使用 NSURLProtocol 来处理每个 NSURLSession 连接的身份验证挑战。我用了

  [NSURLProtocol registerClass:[CustomHTTPSProtocol class]] 

对于 NSURLConnection 。但它不适用于 NSURLSession 所以我需要设置SessionConfiguration.protocolClasses=@[[CustomHTTPSProtocol class]];。问题是我使用这个 URLProtocol 类只处理身份验证挑战,并且我在原始类的代表中收到了数据。

像这样的东西

原班

NSURL连接

 -(void)sendURL
 {
   [NSURLProtocol registerClass:[CustomHTTPSProtocol class]];  //done globally in didFinishLaunching
    self.URL =[NSURL URLWithString:[[NSString stringWithFormat:@"https://www.google.co.in"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:self.URL];
 self.connection=[NSURLConnection connectionWithRequest:request delegate:self];
  }

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
 {
   NSlog(@"received data...%@",data);
  }

NSURLSession

-(void)sendURL
 {
    self.URL =[NSURL URLWithString:[[NSString stringWithFormat:@"https://www.google.co.in"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
  NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:self.URL];
  sessionConfiguration =[NSURLSessionConfiguration defaultSessionConfiguration];
  sessionConfiguration.protocolClasses=@[[CustomHTTPSProtocol class]];
 NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
   NSURLSessionDataTask*task=  [session dataTaskWithRequest:checkInInfoRequest];
    [task resume];
   }

   - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
  {
 NSLog(@"data ...%@  ",data); //handle data here
  }

NSURLProtocol 类

NSURL连接

-(void)startLoading
 {
   NSMutableURLRequest *newRequest = [self.request mutableCopy];
[NSURLProtocol setProperty:@YES forKey:CustomHTTPSProtocolHandledKey inRequest:newRequest];

  self.connection=[NSURLConnection connectionWithRequest:newRequest delegate:self];

 }

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

if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
{
    [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

}
else
{
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

}

NSURLSession

 - (void) startLoading {

NSMutableURLRequest *newRequest = [self.request mutableCopy];
[NSURLProtocol setProperty:@YES forKey:CustomHTTPSProtocolHandledKey inRequest:newRequest];

NSURLSession*session=[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
self.sessionTask=[session dataTaskWithRequest:newRequest];
[self.sessionTask resume];
 }

 - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler{

if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){

    NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
    completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
   }
 }

使用上述逻辑,我能够全局处理所有请求的身份验证并单独处理响应。但是如果我使用 NSURLProtocol,则使用 NSURLSession,身份验证是在协议类中完成的,但我无法接收数据,因为没有调用我的原始类委托。

有人帮帮我。

4

1 回答 1

0

当您实现自己发出请求(或像您一样重新发送)时,您的协议将成为挑战发送者。你负责创建一个NSURLAuthenticationChallenge对象,将自己设置为发送者,提供来自现有挑战的所有其他位,并使用NSURLProtocolClient类发送它。

查看 Apple 的CustomHTTPProtocol 示例代码项目以获取示例和更多信息。

于 2015-10-28T20:11:01.513 回答