// 1.创建请求
NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/MJServer/order"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
// 2.设置请求头
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
// 3.设置请求体
NSDictionary *json = @{
@"order_id" : @"123",
@"user_id" : @"789",
@"shop" : @"Toll"
};
// NSData --> NSDictionary
// NSDictionary --> NSData
NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
request.HTTPBody = data;
// 4.发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"%lu", (unsigned long)data.length);
}];
NSURLConnection — POST-JSON
最新推荐文章于 2020-12-24 08:35:52 发布