簡體   English   中英

iPhone模擬器上的代理授權

[英]proxy authorization on iPhone simulator

有這樣的問題,希望您能對我有所幫助。 這是我正在使用的代碼:

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.

我怎么解決這個問題?? 謝謝!

撥打電話時,您似乎需要發送身份驗證憑據。 嘗試使用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
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM