网络数据请求

#import "ViewController.h"

@interface ViewController ()//用来拼接data

@property(nonatomic,strong)NSMutableData *totadata;

//storybod的四个button

- (IBAction)getaction:(UIButton *)sender;

- (IBAction)postaction:(UIButton *)sender;

- (IBAction)delegateaction:(UIButton *)sender;

- (IBAction)blockaction:(UIButton *)sender;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

#pragma mark

//建立同步请求  get请求

- (IBAction)getaction:(UIButton *)sender {

//1.封装网址对象

NSString *urlstring=@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";

NSURL *url =[NSURL URLWithString:urlstring];

//2.设置网络请求

//    NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:url];

//    [request setHTTPMethod:@"GET"];//设置网络请求方式 get或者post 默认是GET (如果不设置就是默认)

NSURLRequest *request =[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];

//3.连接服务器发送请求

NSData *date =[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSString *resultstring =[[NSString alloc]initWithData:date encoding:NSUTF8StringEncoding];

NSLog(@"%@",resultstring);

/*  NSURLRequest 初始化方法第一个参数:请求访问路径的,第二个参数缓存协议,第三个参数网络请求超时时间(s秒)

其中缓存协议是个枚举类型

NSURLRequestUseProtocolCachePolicy = 0,          (基础策略)

NSURLRequestReloadIgnoringLocalCacheData = 1,    (忽略本地缓存)

NSURLRequestReturnCacheDataElseLoad = 2,        (首先使用缓存,如果没有再去请求)

NSURLRequestReturnCacheDataDontLoad = 3,        (使用本地缓存也不去下载,如果没有本地缓存,也不去再请求.多用于离线操作)

NSURLRequestReloadIgnoringLocalAndRemoteCacheData = 4, // Unimplemented(无视任何缓存策略,无论是本地还是远程的,都要去请求数据)

NSURLRequestReloadIgnoringCacheData = NSURLRequestReloadIgnoringLocalCacheData,

NSURLRequestReloadRevalidatingCacheData=5(如果本地缓存是有效的则不下载,否则都是从服务器重新下载)*/

}

#pragma mark  post

//建立同步post请求

- (IBAction)postaction:(UIButton *)sender {

//1.封装网址对象

NSString *urlstring =@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";

NSURL *url =[NSURL URLWithString:urlstring];

//2.设置网络请求post

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];

//3.设置参数

NSString *poststring =@"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";

NSData *postdata =[poststring dataUsingEncoding:NSUTF8StringEncoding];

[request setHTTPBody:postdata];

//4.建立同步请求,请求数据

NSData *data =[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSString *resultstring =[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"%@",resultstring);

}

#pragma mark ----nsurlconectiondatadelegate-----

//服务器开始响应,准备开始返回数据

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

}

//客户端接收数据

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

self.totadata =[NSMutableData data];

//可变data的拼接方法

[self.totadata appendData:data];

}

//数据接收完成

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{

NSString *string  =[[NSString alloc]initWithData:self.totadata encoding:NSUTF8StringEncoding];

NSLog(@"%@",string);

}

//错误提示,连接失败

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

NSLog(@"%@",error);//一般是网络问题或者是服务器问题造成的

}

//代理实现异步

- (IBAction)delegateaction:(UIButton *)sender {

NSString *urlstring =@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";

NSURL *url =[NSURL URLWithString:urlstring];

//2.

//NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:url];

NSURLRequest *request =[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];

//3建立同步连接

NSURLConnection *connection=  [NSURLConnection  connectionWithRequest:request delegate:self];

}

//  异步请求

- (IBAction)blockaction:(UIButton *)sender {

NSString *urlstring=@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";

NSURL *url =[NSURL URLWithString:urlstring];

//2.

NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:url];

//3.

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

NSString *resultstring =[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"%@",resultstring);

}];

}

@end

你可能感兴趣的:(网络数据请求)