Get cookies from NSHTTPURLResponse



我有一个非常奇怪的问题,我正在请求一个URL,我想从中获取cookie,我用这种方式来获取cookie:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
    NSDictionary *fields = [HTTPResponse allHeaderFields];
    NSString *cookie = [fields valueForKey:"Set-Cookie"];
}

但是饼干不完整,缺少一些字段,我已经在邮递员上检查过了,所有的饼干都在那里。

我在启动NSURLRequest时也使用了这种方法。

[request setHTTPShouldHandleCookies:YES];

问题出在哪里?

注意:此问题在iOS上,我有一个Android版本,它工作正常,所有的cookie都在那里。

您是否尝试过以下代码示例,它应该可以工作:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSArray *cookies =[[NSArray alloc]init]; 
    cookies = [NSHTTPCookie 
        cookiesWithResponseHeaderFields:[response allHeaderFields] 
        forURL:[NSURL URLWithString:@""]]; // send to URL, return NSArray
}

@biloshkurskyi.ss 答案是正确的。

我花了半天时间试图找出为什么我的一些cookie没有出现在我的回复中.allHeaderFields在iOS上,但它在Android上(使用相同的服务)。

原因是某些 Cookie 是预先提取并存储在共享 Cookie 存储中。它们不会出现在所有标题字段中。

如果有人需要,这是答案的快速 3 版本:

let request = URLRequest(url: myWebServiceUrl)
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: {
    (data, response, error) in
    if let error = error {
        print("ERROR: (error)")
    } else {
        print("RESPONSE: (response)")
        if let data = data, let dataString = String(data: data, encoding: .utf8) {
            print("DATA: " + dataString)
        }
        for cookie in HTTPCookieStorage.shared.cookies! {
            print("EXTRACTED COOKIE: (cookie)") //find your cookie here instead of httpUrlResponse.allHeaderFields
        }
    }
})
task.resume()

试试这段代码:

for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
{
    NSLog(@"name: '%@'n",   [cookie name]);
    NSLog(@"value: '%@'n",  [cookie value]);
    NSLog(@"domain: '%@'n", [cookie domain]);
    NSLog(@"path: '%@'n",   [cookie path]);
}
for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
{
    NSLog(@"name: '%@'n",   [cookie name]);
    NSLog(@"value: '%@'n",  [cookie value]);
    NSLog(@"domain: '%@'n", [cookie domain]);
    NSLog(@"path: '%@'n",   [cookie path]);
}

第二个代码是:

NSHTTPURLResponse *HTTPResponsesss = (NSHTTPURLResponse *)response;
NSDictionary *fields = [HTTPResponsesss allHeaderFields];
NSString *cookie = [fields valueForKey:@"Set-Cookie"]; // It is your cookie

NSLog(@"%@", cookie);

两个代码都可以正常工作。

它会

起作用。

NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
        NSString *cookieValue = @"";
        NSString *cookieHeaderValue = @"";
        NSString *finalCookieValue = @"";
        
        for (NSHTTPCookie *each in cookieStorage.cookies) {
            if ([each.name isEqualToString:@"ASP.NET_SessionId"]) {
                cookieHeaderValue = [cookieHeaderValue stringByAppendingString:each.name];
                cookieHeaderValue = [cookieHeaderValue stringByAppendingString:@"="];
                cookieHeaderValue = [cookieHeaderValue stringByAppendingString:each.value];
                cookieHeaderValue = [cookieHeaderValue stringByAppendingString:@";"];
            } else {
                cookieValue = [cookieValue stringByAppendingString:each.name];
                cookieValue = [cookieValue stringByAppendingString:@"="];
                cookieValue = [cookieValue stringByAppendingString:each.value];
                cookieValue = [cookieValue stringByAppendingString:@";"];
            }
        }
        
        finalCookieValue = [NSString stringWithFormat:@"%@%@", cookieHeaderValue, cookieValue];

相关内容

  • 没有找到相关文章

最新更新