国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS9蘋果將原h(huán)ttp協(xié)議改成了https協(xié)議的方法

2020-07-26 03:30:11
字體:
供稿:網(wǎng)友

解決方法:

在info.plist 加入key

<key>NSAppTransportSecurity</key><dict><key>NSAllowsArbitraryLoads</key><true/></dict>

下面給大家介紹ios中http 和https 協(xié)議的訪問

最近做個項目,開始采用的是HTTP協(xié)議實(shí)現(xiàn)客戶端和服務(wù)器端的交互,后來需要改成HTTPS協(xié)議。在修改的過程中發(fā)現(xiàn)了一些問題,解決方案如下:

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

事件觸發(fā)

{ 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];//設(shè)置請求方式為get[request setHTTPMethod:@"GET"];//添加用戶會話id[request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];//連接發(fā)送請求finished = false;NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];//堵塞線程,等待結(jié)束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;}//下面兩段是重點(diǎn),要服務(wù)器端單項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];}//處理數(shù)據(jù) - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ } 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 罗定市| 团风县| 栾城县| 称多县| 洮南市| 安陆市| 遂溪县| 云龙县| 敦化市| 河西区| 闻喜县| 萨迦县| 闸北区| 海原县| 出国| 榆中县| 波密县| 阳新县| 六安市| 磐石市| 拉萨市| 双牌县| 宁安市| 德钦县| 称多县| 镇宁| 怀柔区| 浦县| 同心县| 乌海市| 桓台县| 洪湖市| 上杭县| 利辛县| 海兴县| 皮山县| 清徐县| 长沙市| 宜川县| 泸水县| 天全县|