didReceiveAuthenticationChallenge并同时发布数据

时间:2014-01-26 03:43:04

标签: ios authentication post https

我可以设法做didReceiveAuthenticationChallenge并从服务器获得响应。

但是,我有一个问题。我正在为登录页面做。

我们的程序是首先进行身份验证。同时,我们还将一些数据发布到服务器。如果未收到该数据,我们无法继续下一步。

我的问题是我可以设法进行身份验证,并且该流程首先完成。这意味着在我发布数据之前完成身份验证。我想知道如何同时进行身份验证和发布数据。

Curl命令将是这样的。 curl -k --user username:password --data'username = username& password = password'https://ktl.no-ip.biz/test/public/index.php/api/v1

如果未收到发布的数据,

receivedString是{“error”:true,“message”:“没有给出用户名和/或密码。”}

如果收到, receivedString是{“error”:false,“message”:“登录成功。”}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{

NSLog(@"didReceiveAuthenticationChallenge");



if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
{
    NSLog(@"NSURLAuthenticationMethodServerTrust");

    NSURLCredential *credentail = [NSURLCredential
                                   credentialWithUser:@"username"
                                   password:@"password"
                                   persistence:NSURLCredentialPersistenceForSession];


    [[challenge sender] useCredential:credentail forAuthenticationChallenge:challenge];
    [self loginToCustomServer]; //this will post to server


}
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];

}    




-(void)loginToCustomServer 
{
NSLog(@"loginToCustomServer");

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IP_ADDRESS,login_path]]];

NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
                            username.text, @"username",
                            password.text,@"password",
                            nil];


[httpClient postPath:login_path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

    NSLog(@"Request Successful, response '%@'", responseStr);


    responseFromLogin =
    [NSJSONSerialization JSONObjectWithData: [responseStr dataUsingEncoding:NSUTF8StringEncoding]
                                    options: NSJSONReadingMutableContainers
                                      error: Nil];


    NSLog(@"responseFromLogin is %@",responseFromLogin);


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error in loginToCustomServer: %@", error.localizedDescription);
}];


}

0 个答案:

没有答案