Objective-c 网络编程1 Web请求和响应
2015-12-10 00:00
579 查看
// // ViewController.m // 网络编程 // // Created by DC017 on 15/12/9. // Copyright © 2015年 DC017. All rights reserved. // #import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelegate> { UITextField *textfield; UILabel* label; UIButton *button; UIProgressView *progress; NSMutableData * mudata; long long totalLength; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self buju]; } -(void)buju{ textfield =[[UITextField alloc]initWithFrame:CGRectMake(10, 60, 300, 30)]; textfield.layer.borderWidth=2; textfield.layer.cornerRadius=7; textfield.text=@"http://b.zol-img.com.cn/desk/bizhi/image/7/1920x1080/1449126103556.jpg"; textfield.layer.borderColor=[UIColor blackColor].CGColor; [self.view addSubview:textfield]; progress=[[UIProgressView alloc]initWithFrame:CGRectMake(10, 100, 300,2 )]; progress.backgroundColor=[UIColor whiteColor]; [self.view addSubview:progress]; label =[[UILabel alloc]initWithFrame:CGRectMake(10, 130, 100, 20)]; label.textColor=[UIColor blackColor]; label.text=@"请下载"; [self.view addSubview:label]; button=[[UIButton alloc]initWithFrame: CGRectMake(40, 400, 50, 20)]; [button setTitle:@"下载" forState:UIControlStateNormal]; [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; button.layer.borderWidth=1; button.layer.borderColor=[UIColor redColor].CGColor; button.layer.cornerRadius=8; [self.view addSubview:button]; [button addTarget:self action:@selector(xiazai) forControlEvents:UIControlEventTouchUpInside]; } -(void)xiazai{ NSLog(@"下载"); NSString * tri=textfield.text; //新版 //tri=[tri stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]; //旧版 tri=[tri stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url=[NSURL URLWithString:tri]; //创建请求 NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url cachePolicy:0 timeoutInterval:15.0f]; //创建连接 NSURLConnection * Connection=[[NSURLConnection alloc]initWithRequest:request delegate:self]; //启动连接 [Connection start]; } #pragma mark -开始代理 的四种方法 #pragma mark 开始接收响应 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ NSLog(@"接收响应"); mudata=[[NSMutableData alloc]init]; //初始化进度条 progress.progress=0; NSLog(@"%@",response); //处理响应; //通过响应头中 NSHTTPURLResponse * httpurl=(NSHTTPURLResponse *)response; NSDictionary * head=[httpurl allHeaderFields]; NSLog(@"%@",head); totalLength =[head [@"Content-Length"]longLongValue]; NSLog(@"%lld",totalLength); } #pragma mark 接收数据 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ NSLog(@"接收数据"); [mudata appendData:data]; //更新进度条 [self gengxinjindutiao]; } #pragma mark 数据接收完成 -(void)connectionDidFinishLoading:(NSURLConnection *)connection{ NSLog(@"数据接收完成"); //数据保存苹果官方要求只能保存到缓存里 NSString *path=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject]; NSLog(@"%@",path); path=[path stringByAppendingPathComponent:@"下载图片01.jpg"]; //保存下载内容 [mudata writeToFile:path atomically:YES]; } #pragma mark 请求数据失败 -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"请求失败 原因是:%@",error); } -(void)gengxinjindutiao{ if (mudata.length==totalLength) { label.text=@"下载完成"; } else{ label.text=@"正在下载"; } progress.progress=(float)mudata.length/totalLength; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- Objective-c 网络编程2 Web请求和响应
- https 访问报错 hostname in certificate didn't match
- Apache2.2和Apache2.4中httpd.conf配置文件的异同
- wampserver 打造https 站点(下)
- android中的网络请求(以注册登陆为例)
- WampServer 打造 https 站(上) 图文详解
- 通信协议——Http、TCP、UDP
- 在asyncTask里执行asyncHttp操作报错的原因
- 网络基本知识(1) ARP解析
- Android中判断当前网络是否可用
- Qt:基于TCP和UDP的局域网P2P(局域网)通讯封装
- Lucene + Hadoop 分布式搜索运行框架 Nut 1.0a9转自http://www.linuxidc.com/Linux/2012-02/53113.htm
- 【计算机网络复习】:子网划分
- 10018---HttpClient 基本使用
- WIFI菠萝 strip-n-inject插件安装与使用
- iOS开发——网络连接判断
- BP神经网络
- 使用httpclient 调用selenium webdriver
- 计算机网络笔试面试常考
- laodRuner中Http协议接口编写