我在服務器上有一個API,我試圖從中獲取JSON響應。我已經使用了幾個請求工具來模擬一個調用,並且每次都得到正確的數據。以下是我的請求設置:iOS http POST請求
NSString *post = [NSString stringWithFormat:@"user_id=%@&last_sync=%@",user_id, last_sync];
NSURL *directoryURL = [NSURL URLWithString:directoryURI];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:directoryURL];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[NSData dataWithBytes:[post UTF8String] length:[post length]]];
我的模擬請求中的內容類型也相同。沒有錯誤返回,只是沒有內容。
'@ 「USER_ID =%@&last_sync =%@」'這不是POST這是查詢字符串 – meda
我轉換字符串轉換爲數據並將其傳遞給HTTPBody,如下所述:http://developer.apple.com/library/ios/#documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/WorkingWithHTTPAndHTTPSRequests/WorkingWithHTTPAndHTTPS Requests.html –
如果你有一個JSON API,那麼你的頭應該看起來像這樣:[request setValue:@「application/json」forHTTPHeaderField:@「Accept」]; [request setValue:@「application/json」forHTTPHeaderField:@「Content-Type」];' – meda