1

目的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
4

2 に答える 2

0

X-CSRF-Token を取得してヘッダーに入れる必要があります。

ログイン後、ブラウザーからトークンを取得するには、http: //yoursite.com/services/session/tokenにアクセスします。

次に、すべてのリクエストのヘッダーにトークンを挿入します。

X-CSRF-Token=<token>

参照: iOS で CSRF トークンを取得するには?

于 2013-10-13T02:43:06.557 に答える