ios - 在 header 字段中设置 Cookie

标签 ios objective-c drupal drupal-7 drupal-modules

我正在用 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 like FedAuth and rtFa.

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/

相关文章:

objective-c - Xcode 4.4 中的链接器错误

objective-c - 在 Swift 中识别层内的对象

objective-c - 为什么 UIScrollview 在 Interface Builder 中没有辅助功能区域?

php - 来自 Cron 的 Ubuntu 错误和电子邮件?

drupal - 允许非站点管理员通过管理员菜单访问清除缓存,Drupal 6

ios - UINavigationController 问题中的 TUITabBarController

ios - 除了推送来唤醒 iOS 应用程序之外的其他方式?

ios - Swift 3 "updateChildValues"方法失败,返回 : Invalid key in object. 键必须为非空且不能包含 '.' '#' '$' '[' 或 ']' '

iphone - 如何在 iOS 中释放 subview

php - 获取指定节点类型的最新 10 篇文章?