UI-网络编辑
- 一丶HTTP协议的概念
- HTTP超文本传输协议 应用层协议,由请求和响应构成,是一个标准的客户端服务器,是一种无状态协议
- 两台计算机之间进行通信所必须共同遵守的规定和规则,共同遵守.
- 灵活:HTTP允许传输任意类型的数据对象。正在传输的类型由Content-Type加以标记。
- 无连接:无连接的含义是限制每次连接只处理一个请求。服务器处理完客户的请求,并收到客户的应答后,即断开连接。采用这种方式可以节省传输时间。
- 无状态:HTTP协议是无状态协议。无状态是指协议对于事务处理没有记忆能力。缺少状态意味着如果后续处理需要前面的信息,则它必须重传,这样可能导致每次连接传送的数据量增大。另一方面,在服务器不需要先前信息时它的应答就较快。
Client和Server常常分别相距很远的两台计算机上,Client程序的任务是将用户的要求提交给Server程序,再将Server程序返回的结果以特定的形式显示给用户,Server程序的任务是接受客户程序提出的服务请求,进行相应的处理,在将结果返回给客户程序.
- 二丶HTTP协议常见请求方式
- 网络请求地址对象NSURL的作用及用法
- 网络请求对象NSURLRequest. NSMutableURLRequest的作用及用法
- 网络连接对象NSURLConnection的作用及用法
- 网络连接协议NSURLConnectionDelegate的作用及用法
- 三丶iOS平台如何实现HTTP协议请求
同步会造成主线程 阻塞, 异步不会
GET请求:
- (IBAction)getButton:(id)sender
{NSString *urlStr = @"http://cdn.gq.com.tw.s3-ap-northeast-1.amazonaws.com/userfiles/images_A1/6954/2011100658141857.jpg";
NSURL *url = [[NSURL alloc]initWithString:urlStr];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
//NSURLConnection 连接
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error
];
if (data != nil) {
self.imageView.image =
[UIImage imageWithData:data];
}
NSLog(@"reponse = %@",response);
}
POST请求:
- (IBAction)POST:(id)sender
{NSString *urlStr = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
NSString *postStr = @"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213";
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
//设置请求方法 默认的是GET ,
[request setHTTPMethod:@"POST"];
//转成二进制
NSData *postData = [postStr dataUsingEncoding:NSUTF8StringEncoding];
//调用setHTTP方法
[request setHTTPBody:postData];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *string = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"response === %@",response);
NSLog(@"string == %@",string);
}
GET异步请求:
- (IBAction)getYiButton:(id)sender
{NSString *urlStr = @"http://pic13.nipic.com/20110313/2686941_211532129160_2.jpg";
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
[request setHTTPMethod:@"GET"];
//发起异步连接
[NSURLConnection connectionWithRequest:request delegate:self];
}
//第一步:响应请求
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//请求服务器, 在响应成功后, 进行初始化 得到data对象
NSLog(@"response = %@",response);
self.receiveData = [NSMutableData data];
}
//第二步:服务器给客户端传输 数据
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//后面的data是由服务器传输过来的
NSLog(@"%s,%d",__FUNCTION__,__LINE__);
[self.receiveData appendData:data];
}
//第三步:异步连接数据请求完成之后的方法
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
self.imageView.image = [UIImage imageWithData:self.receiveData];
}
//第四步:当请求发生错误
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//输出错误即可
NSLog(@"error = %@",[error localizedDescription]);
}
异步连接, 给代理发消息,需要代理去执行
BLOCK 异步解析
NSString *urlStr = @"http://pic13.nipic.com/20110313/2686941_211532129160_2.jpg";NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response,NSData *data,NSError *error){
NSLog(@"response = %@",response);
self.imageView.image = [UIImage imageWithData:data];
}];
版权声明:本文为博主原创文章,未经博主允许不得转载。
浙公网安备 33010602011771号