#pragma mark - 基本概念
// NSURLConnection就是从客户端请求发送给服务器
//并且接收服务器返回来的数据
//1.创建请求URL对象 - 设置请求路径
//2.创建请求对象 - 包含请求头、请求体和请求行 这里面
//某些和http相关的内容,由系统自动设置
//我们只需要设置一些必要的内容,比如请求路径、请求体和请求类型;
//3.发送请求NSURLConnection去发送请求
#import "ViewController.h"
#define PATH1 @"http://www.1000phone.net:8088/app/iAppFree/api/limited.php"//?page=1&number=4
//只支持post请求
#define PATH2 @"https://comic.idreamcastle.com/api/register/?access_token=ecpzcknd9wv92hwoafgbj0r28w24h9xl"
@interface ViewController ()<NSURLConnectionDataDelegate,NSURLConnectionDelegate,NSURLConnectionDownloadDelegate>{
//专门用来拼接服务器返回的数据段
NSMutableData *_mdata;
}
@end
@implementation ViewController
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self test3];
}
#pragma mark -使用NSURLConnection做post请求
- (void) test3{
//1.创建URL
NSURL *url = [NSURL URLWithString:PATH2];
//2.创建一个请求对象
//post必须使用NSMutableURLRequest
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//设置请求方式(GET/POST)
//设置为post请求
[request setHTTPMethod:@"POST"];
//设置请求体
//参数:username.password
//a.将要传的数据拼接成字符串(参数名1=参数1的值&参数名2=参数2的值...)
NSString *postStr = @"username=13221212121&password=123456";
//b.将字符串转换成二进制
NSData *postData = [postStr dataUsingEncoding:NSUTF8StringEncoding];
//c.设置请求对象的请求体
//把参数放到请求体
[request setHTTPBody:postData];
//d.设置超时时间(单位是s)
request.timeoutInterval = 30;
//3.发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[
NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//打印服务器的响应
NSLog(@"%@",response);
//打印服务器返回的数据
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",dict);
//NSLog(@"%@",)
}];
}
#pragma mark - 协议方法获取服务器返回的数据
- (void)test2{
//1.创建URL
NSURL *url= [NSURL URLWithString:[PATH1 stringByAppendingString:@"?page=1&number=4"]];
//2.通过url创建请求对象
//NSURLRequest默认是GET方式请求(可以将参数拼接在url,
//通过?和&隔开);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//3.发送网络请求
//参数1:需要发送的请求
//参数2:设置代理
NSURLConnection *connection = [NSURLConnection
connectionWithRequest:request delegate:self];
//4.手动发送请求
[connection start];
}
#pragma mark - NSURLConnection DataDelegate
//当服务器接收到客户端的请求后返回给客户端的响应
//告诉客户端是否成功,请求的数据的相关的信息,但是并没有开始返回数据
//参数1:委托
//参数2:响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"接收到服务器的响应");
NSLog(@"%@",response);
//在这里初始化_mdata;
if (_mdata ) {
//如果_mdata已经接收过数据,就清空_mdata
_mdata.length = 0;
}else{
//如果_mdata为空就初始化_mdata;
_mdata = [[NSMutableData alloc]init];
}
}
//当服务器向客户端返回数据的时候会调用这个方法
//当服务器返回给客户端的数据比较大的时候
//这个方法可能会被调用多次
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
//保存数据
[_mdata appendData:data];
NSLog(@"接收到数据");
}
//当服务器把所有的数据都返回给客户端后惠调用这个方法
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
//这时候_mdata中得数据就是服务器传给客户端完整的数据
//在这里解析数据
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:_mdata options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",dict);
NSLog(@"接收到所有数据");
}
#pragma mark - Connection Delegate
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"%@请求失败",error);
}
#pragma mark - 通过block获取服务器返回的数据
- (void)test1{
//1.创建URL
NSURL *url= [NSURL URLWithString:PATH1];
//2.通过URL创建请求对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//3.发送请求
//异步发送网络请求(网络请求在子线程中执行)
//参数1:请求对象
//参数2:参数3block代码段操作需要添加的队列
//参数3:请求完成后,获取服务器返回给客户端的数据,在这儿可以对拿到的
//服务器的数据进行操作
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//block参数:
//参数1:服务器返回给客户端的响应,包含请求是否成功、请求数据类型、请求数据长度;
//参数2:
NSLog(@"%@",response);
//参数2:服务器返回给客户端请求的数据
//NSLog(@"%@",data);
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",dict);
//参数3:错误信息
NSLog(@"%@",connectionError);
}];
}
- (void)viewDidLoad {
[super viewDidLoad];
//[self test1];
}
@end