网络编程-POST同步和异步(block和delegate)
2015-10-04 19:29
465 查看
注:接收到的数据模式和GET的一样,都是news,在此不写了
Post的同步请求方法-需要设置请求方式为:post,也要设置url和body
//POST请求时的url
#define POST_URL @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"
#define POST_BODY @"date=20150921&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"
#pragma mark==============POST 同步请求==================
- (IBAction)postSynchronizationAction:(id)sender
{
//1.创建url
NSURL *url = [NSURL URLWithString:POST_URL];
//2.创建请求对象
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
//设置请求方法
request.HTTPMethod = @"POST";
//3.创建BODY
NSData *bodyData = [POST_BODY dataUsingEncoding:NSUTF8StringEncoding];
//设置请求数据
request.HTTPBody = bodyData;
//4.连接,请求数据
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
/*---------------------数据解析----------------------*/
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSArray *array = dict[@"news"];
_dataArray = [[NSMutableArray alloc] initWithCapacity:5];
for (NSDictionary *dic in array) {
News *news = [News new];
[news setValuesForKeysWithDictionary:dic];
[_dataArray addObject:news];
}
for (News *news in _dataArray) {
NSLog(@"%@",news);
}
}
POST的异步请求-Block
#pragma mark=========================POST 异步请求=======================
- (IBAction)postAsynchorization:(id)sender
{
// //1.设置url对象
NSURL *url = [[NSURL alloc] initWithString:POST_URL];
//
// //2.设置请求对象
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
//设置请求方式
request.HTTPMethod = @"POST";
//设置请求数据
NSData *dataBody = [POST_BODY dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = dataBody;
//接收请求,并作出响应
[NSURLConnection sendAsynchronousRequest:request queue: [NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response,NSData *data,NSError *connectionError){
//解析数据
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
_dataArray = [NSMutableArray arrayWithCapacity:8];
//封装model对象
for (NSDictionary *dic in dict[@"news"]) {
News *news = [News new];
[news setValuesForKeysWithDictionary:dic];
[_dataArray addObject:news];
}
//校验
for (News *news in _dataArray) {
NSLog(@"%@",news);
}
}];
POST的异步请求--delegate方法
#pragma mark=================POST 异步请求 代理方法===================
- (IBAction)postAsynchorizationDelegateAction:(id)sender
{
//1、创建url对象
NSURL *url = [NSURL URLWithString:POST_URL];
//2、创建请求对象
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
//设置请求方法
request.HTTPMethod = @"POST";
//将数据编码成二进制
NSData *data = [POST_BODY dataUsingEncoding:NSUTF8StringEncoding];
//设置请求数据
request.HTTPBody = data;
//3.创建连接
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
//4.连接开始
[connection start];
}
//1.
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//收到服务器响应后开始准备数据
_dataArray = [[NSMutableArray alloc] initWithCapacity:6];
_allData = [[NSMutableData alloc] initWithCapacity:30];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_allData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//解析数据
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:_allData options:NSJSONReadingAllowFragments error:nil];
//根据键值获得对应的数组
NSArray *array = dict[@"news"];
//封装model对象(model对象方便修改和管理,而且model可以加方法)
for (NSDictionary *dic in array) {
News *news = [News new];
[news setValuesForKeysWithDictionary:dic];
[_dataArray addObject:news];
}
//校验
for (News *news in _dataArray) {
NSLog(@"%@",news);
}
}
Post的同步请求方法-需要设置请求方式为:post,也要设置url和body
//POST请求时的url
#define POST_URL @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"
#define POST_BODY @"date=20150921&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"
#pragma mark==============POST 同步请求==================
- (IBAction)postSynchronizationAction:(id)sender
{
//1.创建url
NSURL *url = [NSURL URLWithString:POST_URL];
//2.创建请求对象
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
//设置请求方法
request.HTTPMethod = @"POST";
//3.创建BODY
NSData *bodyData = [POST_BODY dataUsingEncoding:NSUTF8StringEncoding];
//设置请求数据
request.HTTPBody = bodyData;
//4.连接,请求数据
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
/*---------------------数据解析----------------------*/
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSArray *array = dict[@"news"];
_dataArray = [[NSMutableArray alloc] initWithCapacity:5];
for (NSDictionary *dic in array) {
News *news = [News new];
[news setValuesForKeysWithDictionary:dic];
[_dataArray addObject:news];
}
for (News *news in _dataArray) {
NSLog(@"%@",news);
}
}
POST的异步请求-Block
#pragma mark=========================POST 异步请求=======================
- (IBAction)postAsynchorization:(id)sender
{
// //1.设置url对象
NSURL *url = [[NSURL alloc] initWithString:POST_URL];
//
// //2.设置请求对象
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
//设置请求方式
request.HTTPMethod = @"POST";
//设置请求数据
NSData *dataBody = [POST_BODY dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = dataBody;
//接收请求,并作出响应
[NSURLConnection sendAsynchronousRequest:request queue: [NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response,NSData *data,NSError *connectionError){
//解析数据
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
_dataArray = [NSMutableArray arrayWithCapacity:8];
//封装model对象
for (NSDictionary *dic in dict[@"news"]) {
News *news = [News new];
[news setValuesForKeysWithDictionary:dic];
[_dataArray addObject:news];
}
//校验
for (News *news in _dataArray) {
NSLog(@"%@",news);
}
}];
POST的异步请求--delegate方法
#pragma mark=================POST 异步请求 代理方法===================
- (IBAction)postAsynchorizationDelegateAction:(id)sender
{
//1、创建url对象
NSURL *url = [NSURL URLWithString:POST_URL];
//2、创建请求对象
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
//设置请求方法
request.HTTPMethod = @"POST";
//将数据编码成二进制
NSData *data = [POST_BODY dataUsingEncoding:NSUTF8StringEncoding];
//设置请求数据
request.HTTPBody = data;
//3.创建连接
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
//4.连接开始
[connection start];
}
//1.
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//收到服务器响应后开始准备数据
_dataArray = [[NSMutableArray alloc] initWithCapacity:6];
_allData = [[NSMutableData alloc] initWithCapacity:30];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_allData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//解析数据
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:_allData options:NSJSONReadingAllowFragments error:nil];
//根据键值获得对应的数组
NSArray *array = dict[@"news"];
//封装model对象(model对象方便修改和管理,而且model可以加方法)
for (NSDictionary *dic in array) {
News *news = [News new];
[news setValuesForKeysWithDictionary:dic];
[_dataArray addObject:news];
}
//校验
for (News *news in _dataArray) {
NSLog(@"%@",news);
}
}
相关文章推荐
- 网络编程-GET-同步和异步(delegate和block)
- Linux网络编程---信号驱动I/O
- Internet采用的网络协议以及该协议的主要层次结构
- tcpdump分析网络故障的例子
- 如何使用tcpdump来捕获TCP SYN,ACK和FIN包
- Http服务器实现文件上传与下载(四)
- 《TCP/IP详解 卷一:协议》读书笔记--ICMP:Internet控制报文协议
- linux tcpdump命令以及结果分析
- tcpdump例子
- tcpdump安装以及基本使用
- Lighttpd插件链
- HTTPS协议简介
- [转]计算机网络面试题
- HTTPS
- TCP/IP详解卷1:第十一章 UDP用户数据报协议
- iOS9 HTTP 不能正常使用的解决办法
- 常用网络判断
- TinyHttpd代码解析
- 《深入理解Linux网络技术内幕》阅读笔记(十四)
- 计算机网络部分协议