我有一个非常奇怪的问题,我正在请求一个URL,我想从中获取cookie,我用这种方式来获取cookie:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
NSDictionary *fields = [HTTPResponse allHeaderFields];
NSString *cookie = [fields valueForKey:"Set-Cookie"];
}
但是饼干不完整,有一些字段缺失,我在PostMan上检查过,所有的饼干都在那里。
我在启动NSURLRequest
时也使用了这种方法。
[request setHTTPShouldHandleCookies:YES];
问题出在哪里?
注意:这个问题出现在iOS上,我有一个Android版本,它工作正常,所有的cookie都在那里。
答案 0 :(得分:13)
你是否尝试过代码示例,它应该可以工作:
- (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
}
答案 1 :(得分:8)
试试这段代码:
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]);
}
答案 2 :(得分:8)
@biloshkurskyi.ss answer is spot on.
I spent half a day trying to find out why some of my cookies were not appearing in my response.allHeaderFields on iOS but it was there on Android (using the same service).
The reason is because some cookies are extracted in advance and stored in the shared cookie store. They will not appear in allHeaderFields.
Here's the swift 3 version of the answer if anyone needs it:
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()
答案 3 :(得分:2)
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);
两个代码都运行正常。
答案 4 :(得分:0)
它将起作用。
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];