网络中各种代理总结

网络中各种代理总结

一共是三个代理方法

  1. NSURLConnectionDataDelegate 既可以用于NSURLConnection的下载代理,也可以用于HTTPS 的代理

  2. NSURLSessionDownloadDelegate NSURLSession的下载代理

  3. NSURLSessionTaskDelegate 既可以用于NSURLSession 大文件的上传代理 也可以用于HTTPS的代理

具体用法

  • NSURLConnectionDataDelegate
    NSURLConnection下载的正确代理,需要实现下面几个方法
(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
    //在这里初始化流
}

(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    //在这里接收到数据就写入到沙盒中
}

(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    //完成之后,关闭流
}
  • NSURLSessionDownloadDelegate NSURLSession下载的代理,需要实现的代理方法有
(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location{
    //下载完成文件之后在这里做处理,一般是把临时文件里面的内容放入到Cache目录下
}

 (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
      didWriteData:(int64_t)bytesWritten
 totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
    //在这里计算下载进度
}
  • NSURLSessionTaskDelegate 实现大文件上传的代理,需要实现的代理方法
(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
   didSendBodyData:(int64_t)bytesSent
    totalBytesSent:(int64_t)totalBytesSent
totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend{
    // 在这里计算上传进度
}
  • NSURLSessionTaskDelegate 还可以实现NSURLSession HTTPS的代理,需要实现的代理方法
 (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler {

   NSLog(@"%@", challenge.protectionSpace);
    // 判断是否是信任服务器证书,HTTPS的访问基本上都是信任证书
    if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
        // 使用受保护空间中的服务器信任创建凭据
        NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];

        // 通过 completionHandler 告诉服务器信任证书
        completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
    }
}

  • NSURLConnectionDataDelegate 还可以实现NSURLConnection的HTTPS代理方法
 (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

    NSLog(@"%@", challenge.protectionSpace);

    // 判断是否是信任服务器证书
    if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {

        // 创建凭据
        NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];

        // 发送信任告诉服务器
        [challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值