你必須按照以下步驟創建POST數據:
NSDictionary *trips = [NSDictionary dictionaryWithObjectsAndKeys:
@"SYD", @"departure_code",
@"LON", @"arrival_code",
@"2014-01-24", @"outbound_date",
@"2014-01-29", @"inbound_date",
nil];
NSDictionary *postDict = [NSDictionary dictionaryWithObjectsAndKeys:
trips, @"trips",
@"1", @"adults_count",
nil];
NSError *jsonError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDict options:NSJSONWritingPrettyPrinted error:&jsonError];
然後進行POST請求:
NSURL *url = [[NSURL alloc] initWithString:@"http://api.wego.com/flights/api/k/2/searches"];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15];
[urlRequest setHTTPMethod:@"POST"];
const char *parameters = [jsonData bytes];
NSData *postData = [[NSData alloc] initWithBytes:parameters length:strlen(parameters)];
[urlRequest setHTTPBody:postData];
NSURLResponse *response = nil;
NSError *requestError = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&requestError];
NSLog(@"%@", [NSString stringWithUTF8String:[data bytes]]);