ASI
#import "ViewController.h"
#import "ASIHTTPRequest.h"
@interface HMViewController () <ASIHTTPRequestDelegate>
@property (nonatomic, strong) ASIHTTPRequest *request;
@end
@implementation HMViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self asynGet];
}
发送请求
#pragma mark - ASIHTTPRequestDelegate
/**
* 1.开始发送请求
*/
- (void)requestStarted:(ASIHTTPRequest *)request
{
NSLog(@"requestStarted");
}
/**
* 2.接收到服务器的响应头信息
*/
- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders
{
NSLog(@"didReceiveResponseHeaders");
}
/**
* 3.接收到服务器的实体数据(具体数据)
* 只要实现了这个代理方法,responseData\responseString就没有值
*/
//- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data
//{
// NSLog(@"didReceiveData-%@", data);
//}
/**
* 4.服务器的响应数据接收完毕
*/
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSLog(@"requestFinished--%@", [request responseData]);
}
/**
* 5.请求失败
*/
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSLog(@"requestFailed");
}
发送异步的GET请求
/**
* 异步的GET请求
*/
- (void)asynGet
{
// 1.URL
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video"];
// 2.创建一个请求对象
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
request.timeOutSeconds = 15; // 15秒后服务器还没有响应,就算超时
// 设置代理
request.delegate = self;
// 3.开始请求
[request startAsynchronous];
self.request = request;
}
- (void)dealloc
{
// 这句代码为了防止:控制器销毁了,request还回来调用控制器的代理方法,引发野指针
[self.request clearDelegatesAndCancel];
}
发送同步的GET请求
/**
* 同步的GET请求
*/
- (void)synGet
{
// 1.URL
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/Server/video"];
// 2.创建一个请求对象
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
request.timeOutSeconds = 15; // 15秒后服务器还没有响应,就算超时
// 3.开始请求(这行代码会卡主,等待服务器给数据)
[request startSynchronous];
// 4.请求完毕
NSError *error = [request error];
if (error) {
NSLog(@"请求失败---%@", error);
} else {
NSData *data = [request responseData];
// NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSString *str = [request responseString];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"请求成功---%@", dict);
}
}
监听ASI的block请求
监听ASI的请求
1.成为代理,遵守ASIHTTPRequestDelegate协议,实现协议中的代理方法
request.delegate = self;
- (void)requestStarted:(ASIHTTPRequest *)request;
- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders;
- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data;
- (void)requestFinished:(ASIHTTPRequest *)request;
- (void)requestFailed:(ASIHTTPRequest *)request;
2.成为代理,不遵守ASIHTTPRequestDelegate协议,自定义代理方法
request.delegate = self;
[request setDidStartSelector:@selector(start:)];
[request setDidFinishSelector:@selector(finish:)];
3.设置block
[request setStartedBlock:^{
NSLog(@"setStartedBlock");
}];
[request setHeadersReceivedBlock:^(NSDictionary *responseHeaders) {
NSLog(@"setHeadersReceivedBlock--%@", responseHeaders);
}];
[request setDataReceivedBlock:^(NSData *data) {
NSLog(@"setDataReceivedBlock--%@", data);
}];
[request setCompletionBlock:^{
NSLog(@"setCompletionBlock");
}];
[request setFailedBlock:^{
NSLog(@"setFailedBlock");
}];
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.URL
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video"];
// 2.创建一个请求对象
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
// 3.开始请求
[request startAsynchronous];
// 4.设置监听方法
request.delegate = self;
[request setDidStartSelector:@selector(start:)];
[request setDidFinishSelector:@selector(finish:)];
}
- (void)start:(ASIHTTPRequest *)request
{
NSLog(@"start--%@", request);
}
- (void)finish:(ASIHTTPRequest *)request {
NSLog(@"finish--%d %@ %@", [request responseStatusCode], [request responseStatusMessage], [request responseData]);
}
- (void)asyncBlock
{
// 1.URL
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/Server/video"];
// 2.创建一个请求对象
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
// 如果同时设置了block和实现了代理方法,请求过程中,block和代理方法都会调用,
// 一般的调用顺序:代理方法 > block
// request.delegate = self;
// 3.开始请求
[request startAsynchronous];
// 4.设置block监听
[request setStartedBlock:^{
NSLog(@"setStartedBlock");
}];
[request setHeadersReceivedBlock:^(NSDictionary *responseHeaders) {
NSLog(@"setHeadersReceivedBlock--%@", responseHeaders);
}];
[request setDataReceivedBlock:^(NSData *data) {
NSLog(@"setDataReceivedBlock--%@", data);
}];
[request setCompletionBlock:^{
NSLog(@"setCompletionBlock");
}];
[request setFailedBlock:^{
NSLog(@"setFailedBlock");
}];
}
文件下载
#import "ViewController.h"
#import "ASIHTTPRequest.h"
@interface HMViewController () // <ASIProgressDelegate>
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
@property (nonatomic, assign) BOOL downloading;
@property (nonatomic, strong) ASIHTTPRequest *request;
@end
@implementation HMViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self download];
}
- (void)download
{
if (self.downloading) {
[self.request clearDelegatesAndCancel];
self.downloading = NO;
} else {
// 1.URL
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/Server/resources/test.mp4"];
// 2.创建一个请求对象
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
// 3.设置文件的缓存路径
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *filepath = [caches stringByAppendingPathComponent:@"test.mp4"];
request.downloadDestinationPath = filepath;
// 4.设置进度监听的代理(要想成为进度监听代理,最好遵守ASIProgressDelegate协议)
request.downloadProgressDelegate = self.progressView;
// 这个属性设置为YES,就会支持断点下载
request.allowResumeForFileDownloads = YES;
// 如果要实现断点续传,需要设置一个文件的临时路径
request.temporaryFileDownloadPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"test.temp"];
// 5.开始请求
[request startAsynchronous];
self.request = request;
self.downloading = YES;
}
}
- (void)setProgress:(float)newProgress
{
// NSLog(@"下载进度--%f", newProgress);
self.progressView.progress = newProgress;
}
@end
文件上传
#import "ViewController.h"
#import "ASIFormDataRequest.h"
#import "ASINetworkQueue.h"
@interface HMViewController ()
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
@end
@implementation HMViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// ASINetworkQueue *queue = [ASINetworkQueue queue];
// queue.shouldCancelAllRequestsOnFailure = YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.URL
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/Server/upload"];
// 2.创建一个请求对象
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
// 3.设置请求参数
[request setPostValue:@"zhangsan" forKey:@"username"];
[request setPostValue:@"123" forKey:@"pwd"];
[request setPostValue:@"28" forKey:@"age"];
[request setPostValue:@"1.89" forKey:@"height"];
// 设置文件参数
NSString *file = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];
// 如果知道文件路径,最好就用这个方法(因为简单)
// ASI内部会自动识别文件的MIMEType
[request setFile:file forKey:@"file"];
[request setFile:file withFileName:@"basic.pptx" andContentType:@"application/vnd.openxmlformats-officedocument.presentationml.presentation" forKey:@"file"];
// 如果文件数据是动态产生的,就用这个方法(比如刚拍照完获得的图片数据)
// [request setData:<#(id)#> withFileName:<#(NSString *)#> andContentType:<#(NSString *)#> forKey:<#(NSString *)#>];
// 4.设置监听上传进度的代理
request.uploadProgressDelegate = self.progressView;
// 5.开始请求
[request startAsynchronous];
// 6.监听完毕
__weak typeof(request) weakRequest = request;
[request setCompletionBlock:^{
NSLog(@"%@", [weakRequest responseString]);
}];
}
@end
时间: 2024-11-10 16:06:21