标签:
1 // 2 // ViewController.m 3 // PostJsonDemo 4 // 5 // Created by hellovoidworld on 15/1/28. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () 12 - (IBAction)postJson; 13 14 @end 15 16 @implementation ViewController 17 18 - (void)viewDidLoad { 19 [super viewDidLoad]; 20 // Do any additional setup after loading the view, typically from a nib. 21 } 22 23 - (IBAction)postJson { 24 // 1.创建请求 25 NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/acceptJson"]; 26 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 27 // 设置post发送 28 request.HTTPMethod = @"POST"; 29 30 // 2.设置请求头 31 [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 32 33 // 3.设置请求体 34 NSDictionary *json = @{@"name":@"tom", 35 @"age":@"21"}; 36 request.HTTPBody = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil]; 37 38 39 // 4.发送请求 40 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 41 NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]); 42 }]; 43 44 } 45 @end
[iOS 多线程 & 网络 - 2.6] - 使用POST上传JSON数据 & 多值参数
标签:
原文地址:http://www.cnblogs.com/hellovoidworld/p/4257684.html