ASIHttpRequest框架的使用
使用前的注意点:
- ASIHttpRequest框架是在MRC环境下工作的,使用前必须将相关的文件设置为MRC编译方式.
- 需要导入libz.tbd库,这个库是关于压缩/解压缩的类库.
发送同步请求
- ASIHTTPRequest 默认是以get的方式来实现网络请求的.
//ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//ViewController.m
#import "ViewController.h"
#import "ASIHTTPRequest.h"
@interface ViewController ()<ASIHTTPRequestDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//用子线程来发网络请求
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//创建请求对象
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://localhost/demo.json"]];
//发送同步请求
[request startSynchronous];
//在子线程中执行
NSLog(@"come here = %@",[NSThread currentThread]);
//获得请求结果
NSLog(@"%@---%@----%zd",request.responseString,request.responseData,request.responseData.length);
});
}
发送异步请求
- 使用代理方法接收数据
//ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//ViewController.m
#import "ViewController.h"
#import "ASIHTTPRequest.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//通过代理获得网络请求内容
//创建请求对象
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://localhost/demo.json"]];
//发送异步请求
[request startAsynchronous];
request.delegate = self;
//在子线程中执行
NSLog(@"come here = %@",[NSThread currentThread]);
}
#pragma mark -ASIHTTPRequest代理方法
/**
* 接收到服务器响应头信息
*
*/
-(void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders{
NSLog(@"%s--%@",__FUNCTION__,responseHeaders);
}
/**
* 网络请求失败调用
*/
-(void)requestFailed:(ASIHTTPRequest *)request{
NSLog(@"%s",__FUNCTION__);
}
/**
* 接收完成调用
*/
-(void)requestFinished:(ASIHTTPRequest *)request{
NSLog(@"%s",__FUNCTION__);
//获得请求结果
NSLog(@"%@---%@----%zd",request.responseString,request.responseData,request.responseData.length);
//JSON解析
NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:request.responseData options:0 error:NULL]);
}
/**
* 接收到服务器返回的数据调用(可能会调用多次)
* 只要重写了代理的didReceiveData:方法,意味着所有的响应数据都需要程序员自己拼接,此时通过responseString和responseData获得不了数据了。
*/
//-(void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data{
//
// NSLog(@"%s",__FUNCTION__);
//}
@end
发送异步请求
- 通过block获得网络请求的响应
//ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//ViewController.m
#import "ViewController.h"
#import "ASIHTTPRequest.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 通过block获得网络请求的响应
// URL
NSURL *url = [NSURL URLWithString:@"http://localhost/demo.json"];
// 创建请求对象
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
// 设置block,发送请求前执行
[request setStartedBlock:^{
NSLog(@"开始发送请求");
}];
// 注意: block循环引用
__weak typeof(request) weakRequest = request;
//接收响应完成后的回调
[request setCompletionBlock:^{
//接收请求响应
NSLog(@"%@--%zd",weakRequest.responseString,weakRequest.responseData.length);
}];
//接收到响应头部回调
[request setHeadersReceivedBlock:^(NSDictionary *responseHeaders) {
NSLog(@"responseHeaders = %@",responseHeaders);
}];
// 发送异步请求
[request startAsynchronous];
NSLog(@"come here = %@",[NSThread currentThread]);
}
@end
发送异步请求
- 使用自定义方法获得网络响应
//ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//ViewController.m
#import "ViewController.h"
#import "ASIHTTPRequest.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 通过自定义方法获得网络请求的响应
// URL
NSURL *url = [NSURL URLWithString:@"http://localhost/demo.json"];
// 创建请求对象
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
// 发送请求前调用 startRequest
[request setDidStartSelector:@selector(startRequest:)];
// 请求完毕后调用 finish
[request setDidFinishSelector:@selector(finish:)];
// 设置代理 --> 告诉request调用谁的自定义方法
request.delegate = self;
// 发送异步请求
[request startAsynchronous];
NSLog(@"come here = %@",[NSThread currentThread]);
}
#pragma mark -自定义的处理方法
- (void)finish:(ASIHTTPRequest *)request {
// NSLog(@"%s--%@", __FUNCTION__,request);
NSLog(@"%@--%zd",request.responseString,request.responseData.length);
}
- (void)startRequest:(ASIHTTPRequest *)request {
NSLog(@"%s--%@", __FUNCTION__,request);
}
注意:以上所有的请求方式默认都是用get方式.
使用post的方式请求网络数据
- ASIFormDataRequest请求对象默认是以post的方式来实现网络请求
//ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//ViewController.m
#import "ViewController.h"
#import "ASIFormDataRequest.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// URL
NSURL *url = [NSURL URLWithString:@"http://localhost/login.php"];
// 创建请求对象(ASIFormDataRequest是ASIHTTPRequest的子类,默认使用post方式请求网络)
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
//设置请求体,实现登录提交参数,key是服务器对应接收数据的字段
[request setPostValue:@"zhangsan" forKey:@"username"];
[request setPostValue:@"zhang" forKey:@"password"];
//完成回调block
// 注意: block循环引用
__weak typeof(request) weakRequest = request;
[request setCompletionBlock:^{
NSLog(@"%@",weakRequest.responseString);
}];
// 发送异步请求
[request startAsynchronous];
NSLog(@"come here = %@",[NSThread currentThread]);
}
@end
将上面的请求方式改为get方式
//ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//ViewController.m
#import "ViewController.h"
#import "ASIFormDataRequest.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// URL
NSURL *url = [NSURL URLWithString:@"http://localhost/login.php?username=zhangsan&password=zhang"];
// 创建请求对象(ASIFormDataRequest是ASIHTTPRequest的子类,默认使用post方式请求网络)
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
// 设置请求方式
request.requestMethod = @"GET";
//完成回调block
// 注意: block循环引用
__weak typeof(request) weakRequest = request;
[request setCompletionBlock:^{
NSLog(@"%@",weakRequest.responseString);
}];
// 发送异步请求
[request startAsynchronous];
NSLog(@"come here = %@",[NSThread currentThread]);
}
@end
向服务器发送JSON数据
//ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//ViewController.m
#import "ViewController.h"
#import "ASIFormDataRequest.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// URL
NSURL *url = [NSURL URLWithString:@"http://localhost/post/postjson.php"];
// 创建请求对象(ASIFormDataRequest是ASIHTTPRequest的子类,默认使用post方式请求网络)
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSDictionary *product = @{@"productName":@"iOS开发教程"};
//设置请求体
[request setPostBody:[NSJSONSerialization dataWithJSONObject:product options:0 error:NULL]];
//完成回调block
// 注意: block循环引用
__weak typeof(request) weakRequest = request;
[request setCompletionBlock:^{
NSLog(@"%@",weakRequest.responseString);
}];
// 发送异步请求
[request startAsynchronous];
NSLog(@"come here = %@",[NSThread currentThread]);
}
@end
给服务器上传单个文件
//ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//ViewController.m
#import "ViewController.h"
#import "ASIFormDataRequest.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// URL
NSURL *url = [NSURL URLWithString:@"http://localhost/post/upload.php"];
// 创建请求对象(ASIFormDataRequest是ASIHTTPRequest的子类,默认使用post方式请求网络)
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"aaa.png" ofType:nil];
//方式一:
/**
* 参数1:要上传文件的路径
* 参数2:对应服务器接收文件数据的字段
*/
// [request addFile:filePath forKey:@"userfile"];
//方式二:
/**
* 参数1:要上传文件的路径
* 参数2:保存到服务器的文件名
* 参数3:要上传文件的类型(参考下面)
* 参数4:对应服务器接收文件数据的字段
*/
/* Content-Type:告诉服务器当前文件的类型(MIMEType),一般划分格式为:
大类型/小类型
text/plain 纯文本
text/html html文件
text/xml xml文件
image/png
image/gif
image/jpg
application/json
application/octet-stream 任意的二进制数据
*/
[request addFile:filePath withFileName:@"ppp.png" andContentType:@"image/png" forKey:@"userfile"];
//完成回调block
// 注意: block循环引用
__weak typeof(request) weakRequest = request;
[request setCompletionBlock:^{
NSLog(@"%@",weakRequest.responseString);
}];
// 发送异步请求
[request startAsynchronous];
NSLog(@"come here = %@",[NSThread currentThread]);
}
@end
给服务器上传多个文件
//ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//ViewController.m
#import "ViewController.h"
#import "ASIFormDataRequest.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// URL
NSURL *url = [NSURL URLWithString:@"http://localhost/post/upload-m.php"];
// 创建请求对象(ASIFormDataRequest是ASIHTTPRequest的子类,默认使用post方式请求网络)
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"ooo.png" ofType:nil];
NSString *filePath1 = [[NSBundle mainBundle]pathForResource:@"ccc.txt" ofType:nil];
/**
* 参数1:要上传文件的路径
* 参数2:保存到服务器的文件名
* 参数3:要上传文件的类型(参考下面)
* 参数4:对应服务器接收文件数据的字段
*/
/* Content-Type:告诉服务器当前文件的类型(MIMEType),一般划分格式为:
大类型/小类型
text/plain 纯文本
text/html html文件
text/xml xml文件
image/png
image/gif
image/jpg
application/json
application/octet-stream 任意的二进制数据
*/
[request addFile:filePath withFileName:@"ooo.png" andContentType:@"image/png" forKey:@"userfile[]"];
[request addFile:filePath1 withFileName:@"ttt.txt" andContentType:@"text/plain" forKey:@"userfile[]"];
//完成回调block
// 注意: block循环引用
__weak typeof(request) weakRequest = request;
[request setCompletionBlock:^{
NSLog(@"%@",weakRequest.responseString);
}];
// 发送异步请求
[request startAsynchronous];
NSLog(@"come here = %@",[NSThread currentThread]);
}
@end
下载文件
//ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//ViewController.m
#import "ViewController.h"
#import "ASIHTTPRequest.h"
@interface ViewController ()<ASIProgressDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// URL,如果URL中包含中文,需要程序员手动转码
NSString *urlStr = @"http://localhost/post/117文件操作之字符串与二进制.mp4";
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlStr];
// 创建请求对象
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
//设置下载保存路径
NSString *destPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"aaa.mp4"];
[request setDownloadDestinationPath:destPath];
//设置允许断点续传
[request setAllowResumeForFileDownloads:YES];
//设置下载文件保存的临时文件(文件未下载完就临时存放到这个目录下)
NSString *tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"ppp.mp4"];
[request setTemporaryFileDownloadPath:tempPath];
//设置进度代理
request.downloadProgressDelegate = self;
//完成回调block
// 注意: block循环引用
__weak typeof(request) weakRequest = request;
[request setCompletionBlock:^{
NSLog(@"%@",weakRequest.responseString);
}];
// 发送异步请求
[request startAsynchronous];
NSLog(@"come here = %@",[NSThread currentThread]);
}
#pragma mark -ASIProgress代理方法
-(void)setProgress:(float)newProgress{
//直接获取下载进度
NSLog(@"%f",newProgress);
}
@end
ASIHttpRequest框架下载地址: http://download.csdn.net/detail/u010545519/9518169