网络请求
2015-06-12 20:56
337 查看
请求方式分为:get/post 每一种又有 “同步”、“异步方式”和“代理请求”
一、get 请求
二、post 请求
三、代理请求
“`
一、get 请求
// 1.获取 urlstring NSString *urlstrinf = [NSString stringWithFormat:@"%@",GETurl]; // 网址出现汉字的时候,需要加下面这句 urlstrinf = [urlstrinf stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // 2.把 URLstring 转换 为 url NSURL *url = [NSURL URLWithString:urlstrinf]; // 3.请求数据 // 根据 url 去创建NSURLRequest 对象 NSURLRequest *request = [NSURLRequest requestWithURL:url]; // 4.利用 NSURLConnection 去获取数据 //4.1 同步: NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //4.2 异步请求 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *request,NSData *data,NSError *connectionErrol){ self.imageView.image = [UIImage imageWithData:data]; }];
二、post 请求
//1.urlString 一般定义成宏 NSString *baseURLString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"; //2. 创建 url NSURL *url = [NSURL URLWithString:baseURLString]; //3.创建 request (对于 post 来说,创建的是可变的) NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //4.设置请求方式 为POST ,设置 POST 的参数 request.HTTPMethod = @"POST"; // 不设置的话,默认是 GET NSString *bodyString = @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"; NSData *bodyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding]; // 设置 POST 的参数 (HTTPBody) request.HTTPBody = bodyData; //5.去请求数据 ---同步 //6.解析数据 id result = [NSJSONSerialization JSONObjectWithData:receiverDate options:NSJSONReadingAllowFragments error:nil]; //7.请求数据 异步 // 下面 block 里面的 data 是网络 请求 返回的数据 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data ,NSError *connectinError){ id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSLog(@"%@",result); }];
三、代理请求
前面步骤都一样 先要遵循协议: NSURLConnectionDataDelegate 1.根据字符串URLString 创建URL对象 2.根据URL创建request对象(NSMutableURLRequest 或 NSURLRequest ) 3.post方式设置 请求方式和请求体 4..给NSURLConnection设置代理 [NSURLConnection connectionWithRequest:request delegate:self]; 实现代理方法: (1)客户端响应 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 一般在这对接受的data属性初始化 可变的data self.data = [NSMutableData data]; } (2)收到数据 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.data appendData:data]; } (3)接受数据完毕 -(void)connectionDidFinishLoading:(NSURLConnection *)connection { 可以解析数据 } (4)请求失败 -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"%@",error); }
“`
相关文章推荐
- Android网络请求库android-async-http介绍
- 使用UIWebView与html实现网络请求
- Android HttpClient基本使用方法
- ASIHTTPRequest第三方网络请求详解
- 网络链接简单形式
- ASIHttpRequest简单介绍
- IOS判断是否显示网络请求活动标志
- 关于iOS 数据请求ASIHttpRequest方法的总结
- iOS Framework: Introducing MKNetworkKit
- AFNetworking学习笔记
- socket、http协议
- 网络请求失败,点击按钮不响应事件
- AFNetworking与ASIHTTPRequest的区别
- Retrofit发送网络请求和解析json实例
- Android Volley 和 Android-async-http使用
- 网络请求的三层架构
- 关于AFNetworking 类库的一般用法
- ASIHTTPRequest官方文档
- iOS的网路请求
- iOS网络编程(六) NSURLSession详解