zoukankan      html  css  js  c++  java
  • AFNetworking、ASIHTTPRequest中SSL的使用

    首先介绍下AFNetworking中的使用:

    2.0要注意个地方:IOS7及其以后,採用AFHTTPSessionManagerIOS7之前採用AFHTTPRequestOperationManager

    AFHTTPSessionManager为例,代码例如以下:

    <span style="font-size:14px;">AFHTTPSessionManager   *httpClient = [AFHTTPSessionManager manager];
        httpClient.requestSerializer = [AFHTTPRequestSerializer serializer];
        httpClient.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        // SSL Pinning
        NSString *certificatePath = [[NSBundle mainBundle] pathForResource:@"certificate" ofType:@"der"];
        NSData *certificateData = [NSData dataWithContentsOfFile:certificatePath];
        
        AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
        [securityPolicy setAllowInvalidCertificates:YES];
        [securityPolicy setPinnedCertificates:@[certificateData]];
        [securityPolicy setSSLPinningMode:AFSSLPinningModeCertificate];
        
        [httpClient setSecurityPolicy:securityPolicy];
        
        [httpClient GET:@"http://xxxx" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            //失败信息
        }];</span>

    AFHTTPRequestOperationManager为例,代码例如以下:
    <span style="font-size:14px;">AFHTTPRequestOperationManager *httpClient1 = [AFHTTPRequestOperationManager manager];
        httpClient1.requestSerializer = [AFHTTPRequestSerializer serializer];
        httpClient1.responseSerializer = [AFHTTPResponseSerializer serializer];
        // SSL Pinning
        NSString *certificatePath1 = [[NSBundle mainBundle] pathForResource:@"certificate" ofType:@"der"];
        NSData *certificateData1 = [NSData dataWithContentsOfFile:certificatePath1];
        
        AFSecurityPolicy *securityPolicy1 = [[AFSecurityPolicy alloc] init];
        [securityPolicy1 setAllowInvalidCertificates:YES];
        [securityPolicy1 setPinnedCertificates:@[certificateData1]];
        [securityPolicy1 setSSLPinningMode:AFSSLPinningModeCertificate];
        
        [httpClient setSecurityPolicy:securityPolicy1];
        
        [httpClient1 GET:@"http://xxxx" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            //成功信息
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            //失败信息
        }];</span>


    以下来讲下ASI,

    ASIFormDataRequest为例。代码例如以下:

    <span style="font-size:14px;">ASIFormDataRequest * request;
        request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@""]];
        NSData *cerFile = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"d1" ofType:@"cer"]];
        SecCertificateRef cert = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)cerFile);
        NSArray *array = [NSArray arrayWithObjects:(__bridge id)cert,nil];
        [request setClientCertificates:array];
        [request setValidatesSecureCertificate:NO];
        
        [request startSynchronous];</span>



    ASIHTTPRequest的參考http://www.tuicool.com/articles/UN3AveR。代码就不贴了。

    http://blog.iamzsx.me/show.html?

    id=155002

  • 相关阅读:
    中学数学
    XBT Tracker 服务器配置
    【转】>Unity3d动态数据管理(Export AssetBundles)
    [转]MySQL 5.1.56 for Windows XP/Vista/7
    [转]全面理解Unity加载和内存管理
    [转]Unity3D Editor 编辑器简易教程
    Dow falls 97 points, worst showing this year
    [转]Boost库编译后命名方式
    free falling
    01、Direct3D 11 Basics
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/7352353.html
Copyright © 2011-2022 走看看