2014-07-25 55 views
3

當我試圖調用API(AFHTTPRequestOperationManager)進行身份驗證的方法與確切的用戶名和密碼獲得確切的響應和方法是成功的。然後我嘗試用無效的用戶名和密碼,我得到的答覆是失敗。使用AFHTTPRequestOperationManager時的響應失敗

我嘗試ASIHTTPRequest API的同樣的方法工作正常。所以請檢查下面的要求和反應,並告訴我如何解決這個問題。但在AFHTTPRequestOperationManager無效的用戶響應失敗。

ASIHTTPRequest CALL樣品:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[ NSURL URLWithString:[SharedUtil URLByKey:@"A_URL"]]]; 
request.shouldAttemptPersistentConnection=NO; 
[request setRequestMethod:@"POST"]; 
[request addRequestHeader:@"enctype" value:@"application/x-www-form-urlencoded"]; 
[request addRequestHeader:@"Authorization" value:[SharedUtil authorizationHeader ]]; 
[request addRequestHeader:@"Accept" value:@"application/json"]; 

[request setValidatesSecureCertificate:NO]; 

[request setPostValue:@"pa" forKey:@"type"]; 

[request setPostValue:username forKey:@"username"]; 

[request setPostValue:password forKey:@"password"]; 


[request setDelegate:self]; 

[request setTimeOutSeconds:120]; 

[self setTimer]; 
[request setDidFinishSelector: @selector(responseAuthUserSuccess:)]; 
[request setDidFailSelector: @selector(responseAuthUserFailed:)]; 
[post release]; 
// Cancels an asynchronous request 

[networkQueue addOperation: request]; 

響應是ASIHTTPRequest和方法是成功

{ 「錯誤」: 「invalid_grant」, 「ERROR_DESCRIPTION」:「無法驗證用戶sdsdds @ gmail.com「}

使用AFHTTPRequestOperationManager CALL示例:

AFHTTPRequestOperationManager *managerAF = [AFHTTPRequestOperationManager manager]; 

NSDictionary *params = @{ 
          @"type": @"pa", 
          @"username": username, 
          @"password": password 
          }; 
    [managerAF.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    [managerAF.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"enctype"]; 
    [managerAF.requestSerializer setValue:[SharedUtil authorizationHeader] forHTTPHeaderField:@"Authorization"]; 

    managerAF.responseSerializer = [AFJSONResponseSerializer serializer]; 
    managerAF.securityPolicy.allowInvalidCertificates = YES; 
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 



    [managerAF POST:[SharedUtil URLByKey:@"A_URL"] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { 


     NSData* data=[operation.responseString dataUsingEncoding:NSUTF8StringEncoding]; 


     } 
     [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Error: %@", error); 
     [self stopTimer]; 


    }]; 

AFHTTPRequestOperationManager響應和方法是失敗的:

Printing description of error: 
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo=0xd3b7670 {NSErrorFailingURLKey=https://example.com/token, com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0xd29a010> { URL: https://example.com/token } { status code: 400, headers { 
    "Cache-Control" = "no-store"; 
    Connection = "keep-alive"; 
    "Content-Type" = "application/json;charset=UTF-8"; 
    Date = "Fri, 25 Jul 2014 05:15:19 GMT"; 
    Pragma = "no-cache"; 
    Server = "nginx/1.6.0"; 
    "Transfer-Encoding" = Identity; 
    "X-Powered-By" = "Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.2.2 Java/Oracle Corporation/1.7)"; 
} }, NSLocalizedDescription=Request failed: bad request (400)} 

回答

8

你打你的不良區,因爲API你擊球時返回400 HTTP響應代碼和AFNetworking正確解釋,作爲一個錯誤。通過使用失敗塊中提供的AFHTTPRequestOperation對象的responseDataresponseString屬性,您仍然可以從響應中獲取更詳細的數據。

+0

布蘭登羅斯:謝謝你的回答。我得到了responseString – 2vision2

+0

的回覆Brandon Roth:當我在設備中運行時得到了另外一個幫助ld:找不到-lPods clang:錯誤:linker命令失敗,退出代碼1(使用-v查看調用) – 2vision2

+0

lPods問題是椰子樹問題。谷歌周圍,你應該能夠找到解決方案。 – Brandon