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

首頁 > 系統 > iOS > 正文

ios中http 和https 協議的訪問異常的解決方法

2020-02-19 15:53:47
字體:
來源:轉載
供稿:網友

說到IOS系統,相信大家都不陌生,這是我們蘋果手機的系統,每個軟件工程師都有義務保護用戶數據的隱私和安全,今天這篇文章是武林技術頻道小編為大家搜集的ios中http 和https 協議的訪問異常的解決方法,希望對你學習有幫助!

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

通過武林技術頻道小編介紹的ios中http 和https 協議的訪問異常的解決方法,相信大家都有了一定的了解,如需了解更多的相關資訊,請繼續關注武林技術頻道吧!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宜章县| 正蓝旗| 高青县| 藁城市| 阿坝县| 湟源县| 宜春市| 清原| 黎平县| 璧山县| 文安县| 大余县| 广宁县| 遵义市| 建水县| 增城市| 绥中县| 孟连| 十堰市| 出国| 榆中县| 全州县| 呼图壁县| 巴马| 葫芦岛市| 卢氏县| 佛山市| 涟源市| 华宁县| 永春县| 东山县| 阿拉善右旗| 绥芬河市| 三亚市| 岐山县| 泗水县| 常德市| 扎囊县| 安国市| 宁德市| 广饶县|