2014-07-03 32 views
0

我正在使用NTLM服務器認證(SharePoint),我設法使用NSURLConnection和憑證進行GET請求。Objective-C NSUrlConnection:沒有cookie返回

我想要的是從該請求中獲取會話cookie,例如在桌面上,並將它們放到[NSHTTPCookieStorage sharedHTTPCookieStorage]中,以便將來在WebView中進行連接。

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response; 
    NSLog(@"Response: %@", [HTTPResponse allHeaderFields]); // Log below 
    NSArray *cookies = [NSHTTPCookie 
       cookiesWithResponseHeaderFields:[HTTPResponse allHeaderFields] 
       forURL:[NSURL URLWithString:@"https://domain.com"]]; 

    NSLog(@"How many Cookies: %d", cookies.count); // GOT 0 HERE 

    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:[NSURL URLWithString:@"https://domain.com"] mainDocumentURL:nil]; 

} 

我有這樣的反應是不是一個cookie,我認爲(我改變了值):

Response: { 
     "Cache-Control" = "private,max-age=0"; 
     Connection = "Keep-Alive"; 
     "Content-Encoding" = gzip; 
     "Content-Length" = 1536; 
     "Content-Type" = "text/html"; 
     Date = "Thu, 03 Jul 2014 09:00:35 GMT"; 
     Etag = "\"{F1F0AAgregr-41DA-AgergA26-51A51B019910},3\""; 
     Expires = "Wed, 18 Jun 2014 09:00:36 GMT"; 
     "Last-Modified" = "Fri, 02 May 2014 13:35:13 GMT"; 
     MicrosoftSharePointTeamServices = "14.0.0.6120"; 
     "Persistent-Auth" = true; 
     "Public-Extension" = "http://schemas.microsoft.com/repl-2"; 
     ResourceTag = "rt:F1F0AAE2gerge1B019gerg000003"; 
     SPRequestGuid = "975dccbgregergrgbbeff4f5b5"; 
     Server = "Microsoft-IIS/7.5"; 
     Vary = "Accept-Encoding"; 
     "X-MS-InvokeApp" = "1; RequireReadOnly"; 
     "X-Powered-By" = "ASP.NET"; 
     "X-SharePointHealthScore" = 0; 
    } 

我知道餅乾是假設有過期/ max-age的值會話。 你知道我想做什麼是可能的嗎?

謝謝!

回答

1

可以在方法檢查餅乾 - - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response

按照文檔使用此方法 - connection:willSendRequest:redirectResponse:每當連接決定了它必須改變網址,才能繼續加載的請求被調用。這給了委託人一個機會檢查,並在必要時修改請求&檢查響應。

所以使用像 -

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response { 
    NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response; 
    NSLog(@"Response: %@", [HTTPResponse allHeaderFields]); // Log below 
    NSArray *cookies = [NSHTTPCookie 
       cookiesWithResponseHeaderFields:[HTTPResponse allHeaderFields] 
       forURL:[NSURL URLWithString:@"https://domain.com"]]; 

    NSLog(@"How many Cookies: %d", cookies.count); 
} 
+0

謝謝您的回答,我想這個代碼,並且[類HTTPResponse allHeaderFields]爲空,即使響應爲空。這個函數在所有其他函數之前被調用,但是我沒有像在didReceiveResponse – Pull

+0

和didReceiveResponse中收到響應那樣不是空的響應? – rishi

+0

是的,回覆是我在我的問題中發佈的第二個代碼塊。當我嘗試將它轉換爲一個cookie時,它會創建一個空數組 – Pull