iOS9苹果将原http协议改成了https协议的方法
程序员文章站
2023-11-08 21:37:10
解决方法:
在info.plist 加入key
nsapptransportsecurity
解决方法:
在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 { }
上一篇: 蜂蜜凉性还是热性,这几招让你轻松知道
下一篇: 了解日本的美食,让你爱上这个国度