2012-04-23 5 views
1

私は、iOSでDjangoにPOSTを送信し、Django側でユーザを認証してセッションを開始する基本認証システムを作ろうとしています。今私はURLのデータとして値を渡して認証することでユーザー情報を送信できますが、Djangoの応答からセッションデータまたはCookieを取得するにはどうすればよいですか?クッキーを保存または印刷しようとすると、配列が空であることがわかります。私はrequest.requestCookiesとrequest.responseCookiesの両方を試しました。DjangoとASIHttpRequestでのユーザ認証/セッションの使用

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"test_user", @"username", @"pass", @"password", nil]; 

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://127.0.0.1:8000/login/"]]; 

NSError *error; 
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error]; 

if (error) { 
    NSLog(@"ERROR - %@", error.localizedDescription); 
} else { 
    __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
    [request addRequestHeader: @"Content-Type" value:@"application/json; charset=utf-8"]; 
    [request appendPostData:data]; 
    [request setRequestMethod:@"POST"]; 

    [request setCompletionBlock:^{ 

     UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"Login" 
                  message:@"Login was sent" 
                  delegate:nil 
               cancelButtonTitle:nil 
               otherButtonTitles:@"Ok", nil]; 
     [alerView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO]; 

     NSLog(@"RESPONSE: %@", [[NSString alloc] initWithData:request.responseData encoding:NSUTF8StringEncoding]);//, [request.requestCookies objectAtIndex:0]); 
     NSLog(@"COOKIE: %@", [request.requestCookies objectAtIndex:0]); 
     [ASIHTTPRequest addSessionCookie:[request.requestCookies objectAtIndex:0]]; 

    }]; 

答えて

0

わかりましたので、私はこの問題を解決し、それは私のアプリではなかったもので、どのサーバー側で私はデータに入れていたが、正式にはDjangoでログインません(適切なクッキーを返していなかった)ことが判明適切なヘッダを受信する。

関連する問題