iOS9苹果将原http协议改成了https协议的方法

2016-02-19 09:15 8 1 收藏

下面图老师小编跟大家分享一个简单易学的iOS9苹果将原http协议改成了https协议的方法教程,get新技能是需要行动的,喜欢的朋友赶紧收藏起来学习下吧!

【 tulaoshi.com - 编程语言 】

解决方法:

在info.plist 加入key

keyNSAppTransportSecurity/keydictkeyNSAllowsArbitraryLoads/keytrue//dict

下面给大家介绍ios中http 和https 协议的访问

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

最近做个项目,开始采用的是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

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

事件触发

{ 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{ } 

来源:https://www.tulaoshi.com/n/20160219/1590065.html

延伸阅读
ios9开启低电量模式的方法   很多用过iphone的朋友反馈,苹果最不好的一点就是耗电快,但据可靠消息称,ios9系统可以解决此bug,于是很多iphone用户纷纷升级到最新版ios9正式版,那么对于这种省电模式很多人就有疑问了,ios9低电量模式有必要一直开着吗?ios9低电量模式可以自动开启么?ios9低电量模式在哪?怎么开启? 用手机的光感...
ios9 San Francisco字体   ios8与ios9的字体出现些许变化,iOS 9相比iOS 8字体变得更加容易阅读。没错,这就是关键! iOS 9默认使用的是旧金山字体,从而取代iOS 8的Helvetica字体。用户之前多次抱怨Helvetica的字体过细,是阅读非常困难。 苹果的San Francisco字体最初是为Apple Watch智能手表所开发,而现在已经准备...
ICMP简介: 对于熟悉网络的人来说, ICMP是再熟悉不过了. 它同IP协议一样工作在ISO模型的网络层, 它的全称是: Internet Control Message Protocal. 其在网络中的主要作用是: - 主机探测 - 路由维护 - 路由选择 - 流量控制 对于主机探测来说有很多方法,主机某些服务的BANNER,一些使用的应用程序,或者使用工...
更新ios9失败出现白苹果怎么办   如果在更新过程中遭遇白苹果等问题,也不要担心,直接使用DFU模式进行更新即可。进入DFU模式也很简单,在关机的情况下,同时按住电源键和Home键开机,当出现苹果Logo时松开电源键,继续保持按住Home键一段时间,之后iOS设备便会进入DFU模,然后使用数据线连接到iTunes,然后按照iTunes更新ios9系统教...
ios9使用技巧教程   这份新手指南涵盖了ios9的全部最新特性,例如用户界面、原生应用、增强的Spotlight搜索、强化版Siri、3D Touc(www.tulaoshi.com)h等等,刚刚升级到ios9正式版的用户可以借此充分了解新系统。 不过,遗憾的是,苹果目前只放出了英文版用户指南,而且尚未上架iBooks,英文好且感兴趣的用户可以通过下方...

经验教程

599

收藏

51
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部