gpt4 book ai didi

ios - 调用 drupal webservice 从 objective-c 发布数据

转载 作者:行者123 更新时间:2023-11-29 10:53:53 26 4
gpt4 key购买 nike

我有 drupal 网络服务来通过 objective-c 发布(保存)一个项目。我用谷歌搜索,发现我需要为此设置 cookie。所以我做了以下事情:

NSURL *url = [[NSURL alloc] initWithString:@"http://lanetteam.net/kaleio/services/session/token"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSURLResponse *response = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSString* csrf;
csrf = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
NSString *str = [NSString stringWithFormat:@"http://lanetteam.net/kaleio/node?title=%@&body[und][0][value]=%@",_txtTitle.text,txtContent.text];

NSURL *url1 = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request1= [[NSMutableURLRequest alloc] initWithURL:url1];
NSURLResponse *response1 = nil;
[request1 setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request1 addValue:csrf forHTTPHeaderField:@"X-CSRFToken"];

NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
@"Cookie", NSHTTPCookieName,
[NSString stringWithFormat:@"%@=%@", [appDelegate.dictUserProfile valueForKey:@"session_name"],[appDelegate.dictUserProfile valueForKey:@"sessionid"]], NSHTTPCookieValue,
nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties]; //**error here, cookie is returned as nil**

NSArray* cookies = [NSArray arrayWithObjects: cookie, nil];



NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];


[request1 setAllHTTPHeaderFields:headers];

NSData *responseData1 = [NSURLConnection sendSynchronousRequest:request1 returningResponse:&response1 error:nil];

NSMutableArray *jsonArray=[NSJSONSerialization JSONObjectWithData:responseData1 options:NSJSONReadingMutableContainers error:nil];

NSLog(@"data : %@", jsonArray);

上面我已经提到了错误发生的地方。请让我知道我该如何解决这个问题,自从过去很多天以来我一直在尝试这个问题。如果不设置 cookie,我会收到以下错误:

CSRF validation failed

最佳答案

我也遇到同样的问题。设置 token 后,我收到错误 CSRF 验证失败。但我得到了解决方案,我认为在调用任何网络服务之前它不合适。调用对我有用的注销服务

关于ios - 调用 drupal webservice 从 objective-c 发布数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19289587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com