iOS网络学习
2016-02-18 17:31
357 查看
原文
总结4种网络请求:
同步GET
同步POST
异步GET
同步POST
NSURLConnectionDataDelegate
总结4种网络请求:
同步GET
//同步GET // 1.将网址初始化成一个OC字符串对象 NSString *urlStr = [NSString stringWithFormat:@"http://www.baidu.com/%@?%@", @"中国", @"陕西"];//这里的网址是随便写的,具体要根据提供的服务器文档来创建对应的请求。 // 如果网址中存在中文,进行URLEncode NSString *newUrlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // 2.构建网络URL对象, NSURL NSURL *url = [NSURL URLWithString:newUrlStr]; // 3.创建网络请求 NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10]; // 创建同步链接 NSURLResponse *response = nil; NSError *error = nil; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];//注意后面两个参数都是放地址,暂且先设置为nil
同步POST
// 1.根据网址初始化OC字符串对象 NSString *urlStr = [NSString stringWithFormat:@"%@",@"123" ]; // 2.创建NSURL对象 NSURL *url = [NSURL URLWithString:urlStr]; // 3.创建请求 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 4.创建参数字符串对象 NSString *parmStr = @"method=album.channel.get&appKey=myKey&format=json&channel=t&pageNo=1&pageSize=10"; // 5.将字符串转为NSData对象 NSData *pramData = [parmStr dataUsingEncoding:NSUTF8StringEncoding] 4000 ; // 6.设置请求体 [request setHTTPBody:pramData]; // 7.设置请求方式 [request setHTTPMethod:@"POST"]; // 创建同步链接 NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
异步GET
NSString *urlStr = [NSString stringWithFormat:@"http://image.zcool.com.cn/56/13/1308200901454.jpg"]; NSString *newStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:newStr]; NSURLRequest *requst = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10]; //异步链接(形式1,较少用) [NSURLConnection sendAsynchronousRequest:requst queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { self.imageView.image = [UIImage imageWithData:data]; // 解析 NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSLog(@"%@", dic); }];
同步POST
// POST请求 NSString *urlString = [NSString stringWithFormat:@"%@",kVideoURL]; //创建url对象 NSURL *url = [NSURL URLWithString:urlString]; //创建请求 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10]; //创建参数字符串对象 NSString *parmStr = [NSString stringWithFormat:@"method=album.channel.get&appKey=myKey&format=json&channel=t&pageNo=1&pageSize=10"]; //将字符串转换为NSData对象 NSData *data = [parmStr dataUsingEncoding:NSUTF8StringEncoding]; [request setHTTPBody:data]; [request setHTTPMethod:@"POST"]; //创建异步连接(形式二) [NSURLConnection connectionWithRequest:request delegate:self];
NSURLConnectionDataDelegate
// 服务器接收到请求时 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { } // 当收到服务器返回的数据时触发, 返回的可能是资源片段 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { } // 当服务器返回所有数据时触发, 数据返回完毕 - (void)connectionDidFinishLoading:(NSURLConnection *)connection { } // 请求数据失败时触发 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"%s", __FUNCTION__); }
相关文章推荐
- 原生的http请求
- Java IO:网络IO模型
- 脉络清晰的BP神经网络讲解,赞
- iOS开发之网络编程--3、NSURLSessionDataTask实现文件下载(离线断点续传下载)
- 通信协议:HTTP
- Struggling trying to get cookie out of response with HttpClient in .net 4.5
- 教你写Http框架(一)
- Error:Connection timed out: connect. If you are behind an HTTP proxy, please configure the proxy set
- IOS-网络(网页开发-UIWebView,HTML,CSS,JavaScript,OC和JS代码互调)
- USACO 4.2.1 网络流 最高标号预流推进(HLPP)
- PeopleRank从社交网络中发现个体价值
- 【网络流24题】搭配飞行员/飞行员配对方案
- HttpServletResponse
- socket http tcp 等
- iOS9 Xcode7 https解决方法
- 博客新地址 http://blog.devtao.com
- Socket Http
- TCP/IP协议详解学习笔记-- 链路层(包含CentOS6.5下抓包工具tcpdump的使用)
- [神经网络]1.4-Using neural nets to recognize handwritten digits-A simple network to classify ...(翻译)
- VMWare 网络连接模式(bridged、NAT、host-only)详解