我正在用 IOS 连接 drupal。我在登录时收到 cookie。然后我在 URL 请求的 header 中设置 cookie。我收到 Csrf 验证失败。请帮忙 。任何帮助将不胜感激。
appdelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"%@", appdelegate.cookies);
NSString *post =
[[NSString alloc] initWithFormat:@"type=%@&title=%@", @"ss", @"ssd"];
NSData *postData =
[post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[request setValue:[NSString stringWithFormat:@"%d", [postData length]]
forHTTPHeaderField:@"Content-Length"];
[request addValue:appdelegate.cookies forHTTPHeaderField:@"Cookie"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[urlConnection start];
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response {
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error {
receivedData = nil;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if (receivedData) {
NSDictionary *responseDict =
[NSJSONSerialization JSONObjectWithData:receivedData
options:0
error:nil];
NSLog(@"%@", responseDict);
}
receivedData = nil;
}
最佳答案
You can pass cookie like this
First you have to make
NSArray
of your cookies which is used for authentication likeFedAuth
andrtFa
.
let cookieArray = NSArray(objects: cookie1,cookie2)
let cookieHeader = NSHTTPCookie.requestHeaderFieldsWithCookies(cookieArray)
request.allHTTPHeaderFields = NSDictionary(dictionary: cookieHeader)
关于ios - 在 header 字段中设置 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28577572/