ios中http 和https 協議的訪問

來源:互聯網
上載者:User

最近做個項目,開始採用的是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

 

大家也可以參考下面一篇文章

關於在UIwebView中訪問HTTPS網站的幾種方法

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.