目的cを通じてアイテムを投稿(保存)するためのdrupal Webサービスがあります。グーグルで調べたところ、そのために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