ios項目繞過認證訪問https程式

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   os   java   for   sp   

如果是單個的webview或者request請求,在請求的檔案h中直接實現NSURLConnectionDelegate,並在m中添加下列實現下列兩個方法:

 

 

C代碼  
  1. - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {  
  2.     return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];  
  3. }  
  4.   
  5. - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {  
  6.     if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])  
  7.         //if ([trustedHosts containsObject:challenge.protectionSpace.host])  
  8.             [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]   
  9.                  forAuthenticationChallenge:challenge];    
  10.     [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];  
  11. }  
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];}- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])        //if ([trustedHosts containsObject:challenge.protectionSpace.host])            [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]                  forAuthenticationChallenge:challenge];      [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];}

 上述方法很不爽的是每個請求的地方都要加。

 

如果項目中用到了three20,直接修改“TTRequestLoader.m”即可,這個檔案本身就已經實現了NSURLConnectionDelegate,

 

修改如下,只改這一個地方就行,處處可用。

 

 

C代碼  
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////  
  2. - (void)connection:(NSURLConnection *)connection  
  3. didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{  
  4.     //修改  
  5.     if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])  
  6.         [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];  
  7.     [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];  
  8.       
  9.     TTDCONDITIONLOG(TTDFLAG_URLREQUEST, @"  RECEIVED AUTH CHALLENGE LOADING %@ ", _urlPath);  
  10.     [_queue loader:self didReceiveAuthenticationChallenge:challenge];  
  11. }  
  12.   
  13. //添加  
  14. - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {  
  15.     return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];  
  16. }  
///////////////////////////////////////////////////////////////////////////////////////////////////- (void)connection:(NSURLConnection *)connectiondidReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{    //修改    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])        [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];        TTDCONDITIONLOG(TTDFLAG_URLREQUEST, @"  RECEIVED AUTH CHALLENGE LOADING %@ ", _urlPath);    [_queue loader:self didReceiveAuthenticationChallenge:challenge];}//添加- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];}

 

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.