【问题标题】:Objective-C NSUrlConnection : No cookie returnObjective-C NSUrlConnection:没有cookie返回
【发布时间】:2014-08-24 07:53:06
【问题描述】:

我正在使用 NTLM 服务器身份验证 (SharePoint),我设法使用 NSURLConnection 和凭据发出 GET 请求。

我想要的是从该请求中获取会话 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;
    }

我知道 cookie 的 Expires/Max-Age 值是 Session。 你知道我想做的事情是否可行吗?

谢谢!

【问题讨论】:

    标签: ios objective-c sharepoint cookies nsurlconnection


    【解决方案1】:

    您可以在方法中检查 cookie - - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response

    根据文档,此方法用于 - 每当连接确定它必须更改 URL 才能继续加载请求时,都会调用 connection:willSendRequest:redirectResponse:。这为代理提供了检查的机会,并在必要时修改请求并检查响应。

    所以使用like -

    - (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); 
    }
    

    【讨论】:

    • 感谢您的回答,我试过这段代码,[HTTPResponse allHeaderFields] 为空,甚至响应为空。此函数在所有其他函数之前调用,但我没有像 didReceiveResponse 中那样不为空的响应
    • 并且在 didReceiveResponse 中您收到了响应?
    • 是的,响应是我在问题中发布的第二个代码块。当我尝试将其转换为 cookie 时,它​​会创建一个空数组
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多