2015-05-31 199 views
0

我有一個web服務,我可以通過郵遞員實用程序成功進行郵寄呼叫。在Postman設置是 enter image description hereAFNetworking POST請求失敗代碼= -1011(400)

我使用AFNetworking。這是我的代碼:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
NSDictionary *parameters = @{@"offset": @"1", @"csrf-token": @"63b164ea557b4aba694f81520c71972fa43a9772"}; 
manager.requestSerializer = [AFJSONRequestSerializer serializer]; 
[manager POST:@"http://simple.com/" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"JSON: %@", responseObject); 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error: %@", error); 
}]; 

,但我得到這個錯誤:

Error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo=0x7a6c5240 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7a6b5360> { URL: http://simple.com/ } { status code: 400, headers { 
    "Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0, private, must-revalidate"; 
    Connection = "keep-alive"; 
    "Content-Encoding" = gzip; 
    "Content-Type" = "text/html; charset=utf-8"; 
    Date = "Sun, 31 May 2015 15:40:26 GMT"; 
    Expires = "Thu, 19 Nov 1981 08:52:00 GMT"; 
    Pragma = "no-cache"; 
    Server = "nginx/1.6.2"; 
    "Set-Cookie" = "csrf-token=5589461e87b28a538953ccac4168bf1f98b20b1ds%3A40%3A%22f52bda5262260a3ee0a10efcf48a6a49c11e4103%22%3B; path=/, PHPSESSID=803db60816b9f8203fb8feca01c130b6; path=/"; 
    "Transfer-Encoding" = Identity; 
    Vary = "Accept-Encoding,User-Agent"; 
    "X-Powered-By" = "PHP/5.5.17"; 

這是一個沒有AFNetworking:

​​3210

,並再次錯誤:400

請幫忙解決問題

+0

@ilesh我的問題更正我在下面的答案中描述 – runia

回答

0

我解決了他的問題。

服務器與特定的CSRF令牌保持會話的事實。我的錯誤是我在瀏覽器和移動設備上使用了相同的標記。因此,服務器並沒有給出詳細信息,因爲得到錯誤的POST請求。

我的解決辦法:我首先得到了一個服務器CSRF令牌,然後再POST查詢我使用生成的令牌。爲了響應POST請求,我得到了狀態200和我需要的數據。

確實AFNetworking我對這個問題並沒有真正的幫助,所以我不得不使用標準的NSURLConnection(我把代碼放在問題中)。也許將來有人會派上用場!

謝謝大家!祝你們好運!

相關問題