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