iOS9蘋果將原h(huán)ttp協(xié)議改成了https協(xié)議的方法
更新時間:2016年01月08日 13:33:43 投稿:mrr
這篇文章主要介紹了iOS9蘋果將原h(huán)ttp協(xié)議改成了https協(xié)議的方法的相關資料,需要的朋友可以參考下
解決方法:
在info.plist 加入key
<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>
下面給大家介紹ios中http 和https 協(xié)議的訪問
最近做個項目,開始采用的是HTTP協(xié)議實現(xiàn)客戶端和服務器端的交互,后來需要改成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];
//設置請求方式為get
[request setHTTPMethod:@"GET"];
//添加用戶會話id
[request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
//連接發(fā)送請求
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];
}
//處理數(shù)據(jù)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
}
您可能感興趣的文章:
- 使用Feign配置請求頭以及支持Https協(xié)議
- Nexus使用nginx代理實現(xiàn)支持HTTPS協(xié)議
- Spring Boot項目如何同時支持HTTP和HTTPS協(xié)議的實現(xiàn)
- Spring Boot應用程序同時支持HTTP和HTTPS協(xié)議的實現(xiàn)方法
- SpringBoot2.0如何啟用https協(xié)議
- 關于Https協(xié)議和HttpClient的實現(xiàn)詳解
- startssl申請SSL證書 并且配置 iis 啟用https協(xié)議
- Java獲取http和https協(xié)議返回的json數(shù)據(jù)
- Linux下nginx配置https協(xié)議訪問的方法
- apache中使用mod_gnutls模塊實現(xiàn)多個SSL站點配置(多個HTTPS協(xié)議的虛擬主機)
- https協(xié)議詳解
相關文章
AVFoundation AVCaptureSession媒體捕捉
這篇文章主要為大家介紹了ios開發(fā)AVFoundation AVCaptureSession媒體捕捉詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10
iOS開發(fā)使用XML解析網(wǎng)絡數(shù)據(jù)
XML解析其實這個概念出現(xiàn)了算夠久了,以前javaweb什么到處都在用。這邊我們主要大致介紹下,然后在在ios編程如何使用。2016-02-02

