ios下https代理訪問認證問題

來源:互聯網
上載者:User

標籤:

在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代理訪問認證問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.