AFNetworking、ASIHTTPRequest中SSL的使用

來源:互聯網
上載者:User

標籤:validate   fda   class   pinning   示範   ram   top   session   ati   

首先介紹下AFNetworking中的使用:

2.0要注意個地方:IOS7及其以後,採用AFHTTPSessionManager,IOS7之前採用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

AFNetworking、ASIHTTPRequest中SSL的使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.