- 如何使用session發(fā)送HTTPS請(qǐng)求(需使用代理方法)
- (void)session {
// 1.確定URL
NSURL *url = [NSURL URLWithString:@"https://www.12306.cn/mormhweb/"];
// 2.設(shè)置請(qǐng)求對(duì)象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 3.創(chuàng)建會(huì)話(huà)對(duì)象
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
// 4.創(chuàng)建Task
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// 6.解析數(shù)據(jù)
NSLog(@"-- %@ ------- %@ ---", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding], error);
}];
// 5.執(zhí)行Task
[dataTask resume];
}
// 代理協(xié)議
<NSURLSessionDataDelegate>
#pragma mark - NSURLSessionDataDelegate
/**
// 如果發(fā)送的請(qǐng)求是HTTPS的請(qǐng)求,該方法才會(huì)被調(diào)用
@param session 會(huì)話(huà)對(duì)象
@param challenge 質(zhì)詢(xún),挑戰(zhàn)
@param completionHandler 回調(diào)
*/
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
if (![challenge.protectionSpace.authenticationMethod isEqualToString:@"NSURLAuthenticationMethodServerTrust"]) {
return;
}
NSLog(@"%@", challenge.protectionSpace);
// NSURLSessionAuthChallengeDisposition 如何處理證書(shū)
/**
NSURLSessionAuthChallengeUseCredential = 0, 安裝并使用該證書(shū)
NSURLSessionAuthChallengePerformDefaultHandling = 1, 默認(rèn)方式锋爪,該證書(shū)會(huì)被忽略
NSURLSessionAuthChallengeCancelAuthenticationChallenge = 2, 取消請(qǐng)求金矛,忽略證書(shū)
NSURLSessionAuthChallengeRejectProtectionSpace = 3, 拒絕
*/
// NSURLCredential 授權(quán)信息
NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
}
- 一般在開(kāi)發(fā)中一般會(huì)使用第三方框架處理發(fā)送網(wǎng)絡(luò)數(shù)據(jù)葱绒,我們以AFNetworking為例講
- (void)AFN {
// 01.創(chuàng)建會(huì)話(huà)管理對(duì)象
AFHTTPSessionManager *httpSessionManager = [AFHTTPSessionManager manager];
// +// 更改解析方式
httpSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
// +// 設(shè)置對(duì)證書(shū)的處理方式 是否接收無(wú)效證書(shū)
httpSessionManager.securityPolicy.allowInvalidCertificates = YES;
// +// AFN內(nèi)部默認(rèn)會(huì)對(duì)域名進(jìn)行驗(yàn)證 修改是否對(duì)域名進(jìn)行驗(yàn)證
httpSessionManager.securityPolicy.validatesDomainName = NO;
// 02.發(fā)送請(qǐng)求
[httpSessionManager GET:@"https://www.12306.cn/mormhweb/" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"-- %@ ---", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"-- %@ ---", error);
}];
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者