- 什么是网络编程?
为什么要学习网络编程?
网络编程主要做什么事情?
什么是C/S模式?什么又是B/S模式?跟我们有什么关系?
client server
browser server对于我们来说,网络编程难学吗?
在百度输入一个文字搜索后,都做了哪些事情?
// 119.75.217.109
6.1 打开浏览器,输入百度的地址,按下回车
6.2 获取到了数据,浏览器解析数据,显示出来
6.3 输入框输入要搜索的内容:iPhone
6.4 浏览器通过地址,把要搜索的数据发送给了百度的服务器
6.5 服务器根据条件去给你查询数据,之后将数据返回给浏览器
6.6 浏览器又解析数据,并显示请求对象、链接对象都是什么?
请求:GET、POST
链接:同步、异步
GET 同步
GET 异步 Block方式
POST 同步
POST 异步 Delegate实现
请求对象: NSMutableURLRequest
setHTTPMethod:
setHTTPBody:
链接对象: NSURLConnection
sendSync…….
sendAsync…..
connection…..
如果需要数据
0. 准备NSURL
1. 创建请求对象
设置请求方式:GET、POST
如果选择了POST方式,设置请求数据
2. 创建链接对象并发送请求
使用同步方式或者异步方式
//#pragma mark - GET同步
- (IBAction)getSyncButtonAction:(UIButton *)sender
{
// 1. 准备URL
NSURL *url = [NSURL URLWithString:BASE_URL_1];
// 2. 创建请求对象(参数是地址)
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"]; // 设置请求方式
// 3. 创建链接对象并发送请求
// 3.1 保存结果附加信息的对象
NSURLResponse *respone = nil;
// 3.2 保存错误信息的对象
NSError *error = nil;
// 3.3 发送请求,并接受结果
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&respone error:&error];
// 4. 打印
NSLog(@"%lld", respone.expectedContentLength);
NSLog(@"%@", respone.URL);
// 打印错误
NSLog(@"%@", error);
// 解析数据,并显示到页面上
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
_textView.text = [NSString stringWithFormat:@"%@", dict];
}
//#pragma mark - GET异步
- (IBAction)getAsyncButtonAction:(UIButton *)sender
{
// 1. 准备URL
NSURL *url = [NSURL URLWithString:BASE_URL_1];
// 2. 创建请求对象(参数是地址)
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"]; // 设置请求方式
// 3. 创建链接对象,发送请求
__block ViewController *weakSelf = self;
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 4. 解析数据
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
// 5. 使用当前控制器的属性的时候,不能直接写属性名称,照样需要__block修饰的变量去执行
weakSelf.textView.text = [NSString stringWithFormat:@"%@", dict];
}];
}
//#pragma mark - POST同步
- (IBAction)postSyncButtonAction:(UIButton *)sender
{
// 1. 准备网址
NSURL *url = [NSURL URLWithString:BASE_URL_2];
// 2. 准备参数(直接给字符串发送dataUsingEncoding消息,使用UTF-8编码)
NSData *paramData = [BASE_URL_2_PARAM dataUsingEncoding:NSUTF8StringEncoding];
// 3. 创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"]; // 设置请求方式
[request setHTTPBody:paramData]; // 设置参数
// 4. 创建链接对象,并发送请求
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// 5. 解析并显示
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
_textView.text = [NSString stringWithFormat:@"%@", dict];
}
//#pragma mark - POST异步
- (IBAction)postAsyncButtonAction:(UIButton *)sender
{
// 1. 准备网址
NSURL *url = [NSURL URLWithString:BASE_URL_2];
// 2. 准备参数(直接给字符串发送dataUsingEncoding消息,使用UTF-8编码)
NSData *paramData = [BASE_URL_2_PARAM dataUsingEncoding:NSUTF8StringEncoding];
// 3. 创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"]; // 设置请求方式
[request setHTTPBody:paramData]; // 设置参数
// 4. 创建链接对象,并发送请求
[NSURLConnection connectionWithRequest:request delegate:self];
}
//#pragma mark - NSURLConnectionDataDelegate
//#pragma mark 开始接受请求结果
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.allData = [NSMutableData data];
}
//#pragma mark 开始接受数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_allData appendData:data];
}
///#pragma mark 数据接收完毕
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:_allData options:NSJSONReadingAllowFragments error:nil];
_textView.text = [NSString stringWithFormat:@"%@", dict];
}
//#pragma mark 发生错误,执行此方法
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"%@", error);
}
//#pragma mark - 清除
- (IBAction)clearButtonAction:(UIButton *)sender {
_textView.text = nil;
}