我已經爲打印一個演示代碼從Web服務的數據,如何使用NSURLSession從Web服務獲取數據?
代碼
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://54.149.46.64/API/video.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"video" forHTTPHeaderField:@"list"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"requestReply: %@", requestReply);
}] resume];
輸出
JSON_Webservices[4496:150468] requestReply: {"Status":"Fail","Message":"Please enter required fields"}
爲什麼我無法從WS獲取數據?請幫幫我。
做工精細在郵差
圖片
謝謝。
我想你不應該設置forHTTPHeaderField視頻列表。將其設置爲HTTPBody –
如何設置它? @Dheeraj – user7018875
你在POSTMan中傳遞的參數是什麼? –