ios https請(qǐng)求處理方式1:信任一切證書
處理方法
1:使用異步請(qǐng)求方式
2:證書信任的委托這樣處理
-(BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection{
return true;
}
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
NSLog(@"didReceiveAuthenticationChallenge %@ %zd", [[challenge protectionSpace] authenticationMethod], (ssize_t) [challenge previousFailureCount]);
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
[[challenge sender] useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
[[challenge sender] continueWithoutCredentialForAuthenticationChallenge: challenge];
}
}
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace{
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
完整示例代碼
UIViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<NSURLConnectionDataDelegate>
@end
UIViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//string 轉(zhuǎn) url編碼
NSString *urlString = @"https://api.github.com/users/coolnameismy";
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
NSURLConnectionDataDelegate *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[connection start];
}
//請(qǐng)求失敗
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"=================didFailWithError=================");
NSLog(@"error:%@",error);
}
//重定向
- (nullable NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(nullable NSURLResponse *)response{
NSLog(@"=================request redirectResponse=================");
NSLog(@"request:%@",request);
return request;
}
//接收響應(yīng)
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"=================didReceiveResponse=================");
NSHTTPURLResponse *resp = (NSHTTPURLResponse *)response;
NSLog(@"response:%@",resp);
}
//接收響應(yīng)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
NSLog(@"=================didReceiveData=================");
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"data:%@",dic);
}
//- (nullable NSInputStream *)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)request{
//
//}
//上傳數(shù)據(jù)委托,用于顯示上傳進(jìn)度
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten
totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite{
NSLog(@"=================totalBytesWritten=================");
}
//- (nullable NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse{
//
//}
//完成請(qǐng)求
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"=================connectionDidFinishLoading=================");
}
#pragma mark -https認(rèn)證
-(BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection{
return true;
}
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
NSLog(@"didReceiveAuthenticationChallenge %@ %zd", [[challenge protectionSpace] authenticationMethod], (ssize_t) [challenge previousFailureCount]);
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
[[challenge sender] useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
[[challenge sender] continueWithoutCredentialForAuthenticationChallenge: challenge];
}
}
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace{
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
參考文章
-iOS: HTTPS 與自簽名證書
-iOS - HTTPS
-iOS安全系列之一:HTTPS
-iOS安全系列之二:HTTPS進(jìn)階
-Making HTTP and HTTPS Requests