0.前述
- 由于目前苹果在推出NSURLSession后更推荐我们使用它,而此前的NSURLConnection已经在最新的Xcode7上已经过期
- 目前最流行的第三方框架AFNetworking也是基于NSURLConnection和NSURLSession的封装
- 为了更好的理解网络通信的内部实现原理,这里会对基本的NSURLConnection、NSURLSession到后续AFN进行探究,更方便我们理解和使用第三方框架,甚至自己搭建框架等等…
1.网络请求常用类
- NSURL:请求地址
- NSURLRequest:一个NSURLRequest对象就代表一个请求,它包含的信息有
- 一个NSURL对象
- 请求方法、请求头、请求体
- 请求超时
- NSMutableURLRequest:NSURLRequest的子类
- NSURLConnection
- 负责发送请求,建立客户端和服务器的连接
- 发送数据给服务器,并收集来自服务器的响应数据
2.URL中的中文问题
- 若是URL中有中文
- 如果是POST请求, 参数中可以有中文
- 如果是GET请求, URL中不能有中文
- 开发技巧: 无论是get/post, 都对URL进行一次转码
- 示例代码
NSString *urlStr = @"http://120.25.226.186:32812/login?username=用户名&pwd=密码&type=JSON";
[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
request.HTTPBody = [@"username=用户名&pwd=密码&type=JSON" dataUsingEncoding:NSUTF8StringEncoding];
2.NSURLConnection的使用步骤
- 创建一个NSURL对象,设置请求路径
- 传入NSURL创建一个NSURLRequest对象,设置请求头和请求体
- 使用NSURLConnection发送请求
3.同步+GET请求
-(void)sendSync{
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=zj&pwd=123&type=JSON"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSHTTPURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSLog(@"data = %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
NSLog(@"%zd,%@",response.expectedContentLength,response.allHeaderFields);
NSLog(@"%@",[response class]);
}
4.异步+GET请求
-(void)sendAsync{
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=zj&pwd=123&type=JSON"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"%@",[NSThread currentThread]);
}];
}
5.异步+POST请求
-(void)sendPOST{
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
request.HTTPBody = [@"username=zj&pwd=123&type=JSON" dataUsingEncoding:NSUTF8StringEncoding];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}];
}
6.监听请求的具体传输过程
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
}
#pragma mark - NSURLConnectionDataDelegate
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"%zd", response.expectedContentLength);
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
NSLog(@"%zd", data.length);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"%s",__func__);
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"%s",__func__);
}