iOS9苹果将原http协议改成了https协议的方法
2019/7/9 23:24:25
本文主要是介绍iOS9苹果将原http协议改成了https协议的方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
解决方法:
在info.plist 加入key
<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>
下面给大家介绍ios中http 和https 协议的访问
最近做个项目,开始采用的是HTTP协议实现客户端和服务器端的交互,后来需要改成HTTPS协议。在修改的过程中发现了一些问题,解决方案如下:
HTTP:
NSString *urlString =[NSString stringWithFormat:@"https://127.0.0.1/default.aspx?USER=%@",@"111"]; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"GET"]; NSHTTPURLResponse* urlResponse = nil; NSError *error = [[NSError alloc] init]; NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; NSMutableString *result = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; NSLog(@"The result string is :%@",result);
HTTPS
事件触发
{ NSString *urlString =[NSString stringWithFormat:@"https://127.0.0.1/default.aspx?USER=%@",@"111"]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5]; //设置请求方式为get [request setHTTPMethod:@"GET"]; //添加用户会话id [request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"]; //连接发送请求 finished = false; NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; //堵塞线程,等待结束 while(!finished) { [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; } } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response {} - (void)connectionDidFinishLoading:(NSURLConnection *)connection { //[_waitingDialog dismissWithClickedButtonIndex:0 animated:NO]; [connection release]; } -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { } - (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection{ return NO; } //下面两段是重点,要服务器端单项HTTPS 验证,iOS 客户端忽略证书验证。 - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; } - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(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]; } } NSLog(@"get the whole response"); //[receivedData setLength:0]; } //处理数据 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { }
这篇关于iOS9苹果将原http协议改成了https协议的方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-14system bios shadowed
- 2024-03-14gabios
- 2024-02-07iOS应用提交上架的最新流程
- 2024-02-06打包 iOS 的 IPA 文件
- 2023-12-07uniapp打包iOS应用并通过审核:代码混淆的终极解决方案 ?
- 2023-11-25uniapp IOS从打包到上架流程(详细简单) 原创
- 2023-11-10【iOS开发】iOS App的加固保护原理:使用ipaguard混淆加固
- 2023-09-30最强大的iOS应用源码保护工具:Ipa Guard,保护你的商业机密代码
- 2023-09-07iOS安全加固探讨:代码混淆、类名方法名混淆等方法
- 2023-09-05iOS代码加固与保护方法详解 - 提升iOS应用安全性的关键步骤