2011-03-07 35 views
0

有這樣的問題,希望你能幫助我..找不到任何地方。下面是我使用的代碼:iPhone模擬器上的代理授權

NSURL *serverURL = [NSURL URLWithString:@"http://someServer.com/Login"]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:serverURL]; 
[request addValue:@"text/plain" forHTTPHeaderField:@"ACCEPT"]; 
[request setHTTPMethod:@"POST"]; 

[request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData]; 

NSData *data = [encodedString dataUsingEncoding:NSUTF8StringEncoding]; 
[request setHTTPBody:data]; 

URLLoader *loader = [URLLoader loaderWithRequest:request]; 

當我想從電腦都可以在瀏覽器中查看在服務器http://someServer.com/Login

當我嘗試從iPhone模擬器上的safary訪問相同的服務器 - 授權後出現授權窗口一切正常。

但是,當我在iPhone模擬器上以程序方式執行此請求時,我發佈了代碼示例。出現下一個錯誤:

The following error was encountered: 
Cache Access Denied. 

Sorry, you are not currently allowed to request: 
    http://someserver.com/Login 
from this cache until you have authenticated yourself. 

我該如何解決這個問題?感謝名單!

回答

1

似乎您在撥打電話時需要發送身份驗證憑據。 嘗試使用NSURLConnection類撥打電話,設置委託,並實現以下方法

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ 
    if ([challenge previousFailureCount] == 0) { 
     NSURLCredential *newCredential; 
     newCredential=[NSURLCredential credentialWithUser:@"ABC" 
               password:@"XYZ" 
               persistence:NSURLCredentialPersistenceNone]; 
     [[challenge sender] useCredential:newCredential 
       forAuthenticationChallenge:challenge]; 

    } else { 

     [[challenge sender] cancelAuthenticationChallenge:challenge]; 
     // inform the user that the user name and password 
     // in the preferences are incorrect 
    } 

} 
+0

感謝名單,我只是說這個服務器列表中使用它沒有代理。 – yozhik 2011-03-07 14:57:50

+0

有什麼辦法可以使用同步方法對代理和主網站進行身份驗證? – prajul 2012-02-25 20:09:03