ASI
#import "ViewController.h"
#import "ASIHTTPRequest.h"
@interface HMViewController ()
@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 () //
@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