首页 > 代码库 > 同步POST请求
同步POST请求
创建POST请求
1 根据网址初始化OC字符串对象
NSString * urlString = [NSString stringWithFormat:@"%@",kVideoURL];
2 创建URL对象
NSURL * url = [NSURL URLWithString:urlString];
3 创建请求
NSMutableURLRequest * requrst = [NSMutableURLRequest requestWithURL:url];
4 创建参数字符串对象
NSString * parmStr = [NSString stringWithFormat:@"%@",@"method=album.channel.get&appKey=myKey&format=json&channel=t&pageNo=1&pageSize=10"];
5 将字符串转为NSDATA 对象
NSData * parmData = http://www.mamicode.com/[parmStr dataUsingEncoding:NSUTF8StringEncoding];
6 设置请求体
[requrst setHTTPBody:parmData];
7 设置请求方式
[requrst setHTTPMethod:@"POST"];
创建同步连接
NSData * data = http://www.mamicode.com/[NSURLConnection sendSynchronousRequest:requrst returningResponse:nil error:nil];
解析
NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",dic);
同步POST请求