標籤:
在ios下,需要開啟https連結,但是如果其中使用了代理訪問,則會被預設返回認證驗證錯誤,無法正常訪問
通常是在國內訪問國外facebook的情況下
這是因為
https訪問的時候,會驗證一次認證,如果用了代理,認證驗證的時候會被認為有風險,則會拒絕掉串連
也就是為了避免中間人攻擊而做的限制
這裡可以考慮先用NSURLConnection建立一個https串連,讓本次針對目標地址的串連在驗證時忽略認證,就可以保證之後的串連再也沒認證驗證問題了
NSString* strUrl = [NSString stringWithFormat:@https://graph.facebook.com/me]; NSURLRequest* reqUrl = [NSURLRequest requestWithURL:[NSURL URLWithString:strUrl]]; NSURLConnection* conn = [NSURLConnection connectionWithRequest:reqUrl delegate:self]; [conn start]; - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"connection didFailWithError result: %@", error);}- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection{ return NO;}- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ //第一次驗證通過,之後取消驗證 if ([challenge previousFailureCount] ==0) { NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; } else { [[challenge sender] cancelAuthenticationChallenge:challenge]; }}// Deprecated authentication delegates.- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace{ return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];}- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ //第一次驗證通過,之後取消驗證 if ([challenge previousFailureCount] ==0) { NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; } else { [[challenge sender] cancelAuthenticationChallenge:challenge]; }}- (void)connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ }- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ //做你需要做的事情}
不過這個最好看看最新的sdk支不支援了。。
ios下https代理訪問認證問題