你好,歡迎來到IOS教程網

 Ios教程網 >> IOS使用技巧 >> IOS7技巧 >> ios中http 和https 協議的訪問代碼

ios中http 和https 協議的訪問代碼

編輯:IOS7技巧
文章介紹了關於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

{

}

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved