/*

   HTTP 是应用层的网络传输协议,对HTTP请求主要流行的方式有两种 GET请求以及POST请求

   GET请求和POST请求区别和联系:

   1.GET请求,服务器网址以及参数都会出现在我们网络请求的接口中,也就是说参数会作为请求接口的一部分. 而POST请求在接口中只有服务器网址,而参数会作为请求体交给服务器

   2.因为GET请求参数会作为接口的一部分出现在接口中,所以信息容易捕获,不安全.而POST请求参数会被封装在请求体中,作为二进制数据进行传输,不易捕获,安全性高

   3.因为接口有字节限制,虽然说从理论上GET请求和POST请求都可以进行请求数据和上传数据,GET请求参数会占用字节,所以只能进行小数据上传.而对于POST请求参数在请求体二进制数据中,理论上是无限的.所以,我们一般使用GET请求获取数据,POST请求上传数据.

*/

//同步GET

- (IBAction)synGET:(id)sender {

    

    //1.创建NSURL 对象

    //(1)获取urlString

    NSString *urlString = [NSString stringWithFormat:@"%@",kPicURL];

    //(2)重新编码

    NSString *newStr = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    //(3)获取网址

    NSURL *url = [NSURL URLWithString:newStr];

    

    //2.创建NSURLRequest 对象

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    

    //3.请求数据

    //

    NSURLResponse *response = nil;

    NSError *error = nil;

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

    NSLog(@"==%@",response);

    NSLog(@"++%@",error);

    NSLog(@"--%@",data);

    

    self.imageView.image = [UIImage imageWithData:data];

    

    //解析

    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];

    NSLog(@"%@",dic);

}

//同步POST

- (IBAction)synPOST:(id)sender {

    

    //1.创建NSURL对象

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

    

    NSURL *url = [NSURL URLWithString:urlString];

    

    //2.创建可变请求对象

    NSMutableURLRequest *muRequest = [NSMutableURLRequest requestWithURL:url];

    

    //3.设置请求体 和 请求方式

    //(1)创建请求参数

    NSString *parameterStr = [NSString stringWithFormat:@"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"];

    //(2)将请求参数封装成NSDate对象

    NSData *paraData = [parameterStr dataUsingEncoding:NSUTF8StringEncoding];

    //设置请求体 请求方式

    [muRequest setHTTPBody:paraData];

    [muRequest setHTTPMethod:@"POST"];

    

    //4.请求数据

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

    //...数据处理

    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

    

    NSLog(@"%@",dic);

}

//异步GET

- (IBAction)asynGET:(id)sender {

    

    //1.创建NSURL对象

    //(1)创建urlString

    NSString *urlString = [NSString stringWithFormat:kPicURL];

    //(2)重新编码

    NSString *newStr = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    //(3)url

    NSURL *url = [NSURL URLWithString:newStr];

    

    //2.创建request对象

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    

    //3.请求数据

    //a 使用代理方法回调进行异步网络请求

//    [NSURLConnection connectionWithRequest:request delegate:self];

    //b.使用block进行异步网络请求

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

        

        //当数据获取成功时执行

        self.imageView.image = [UIImage imageWithData:data];

    }];

}

//异步POST

- (IBAction)asynPOST:(id)sender {

    

    //1.创建NSURL对象

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

    NSURL *url = [NSURL URLWithString:urlString];

    

    //2.创建可变的请求对象

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    

    //3.设置请求体 和 请求方式

    //(1)获取参数字符串

    NSString *paraString = [NSString stringWithFormat:@"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"];

    //(2)转化为NSData

    NSData *data = [paraString dataUsingEncoding:NSUTF8StringEncoding];

    

    request.HTTPBody = data;

    request.HTTPMethod = @"POST";

    

    //4.请求

    //a 代理方法回调

    [NSURLConnection connectionWithRequest:request delegate:self];

    

    //b block

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

//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

//        NSLog(@"%@",dic);

//    }];

    

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

#pragma mark - connectionDelegate

//连接到服务器

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

{

    //当连接到服务器时 创建对象

    self.data = [NSMutableData data];

    NSLog(@"建立连接");

}

//获取数据时

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

{

    //拼接数据

    [self.data appendData:data];

    NSLog(@"获取数据");

}

//完成请求

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    NSLog(@"请求完成");

    //数据请求完成 可以进行数据的操作

    self.imageView.image = [UIImage imageWithData:_data];

    

    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:_data options:0 error:nil];

    NSLog(@"%@",dic);

}

//请求失败

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

{

    NSLog(@"请求失败");

}

- (void)dealloc {

    [_imageView release];

    [super dealloc];

}

 

posted on 2015-05-28 09:43  hanry  阅读(156)  评论(0)    收藏  举报