ASIHTTPRequest *_request =[ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"www.baidu.com"]];
//开启代理 在代理中捕捉此次请求的具体结果
_request.delegate =self;
//超时时间
_request.timeOutSeconds =30;
//发起请求同步异步的区别就在于调用的方法不同
//startAsynchronous 异步
//startSynchronous 同步
[_request startAsynchronous];
}
//post
-(IBAction)postBtnClick:(id)sender
{
//post注册
ASIFormDataRequest *request =[ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://127.0.0.1:8080/A/a"]];
//追加数据 前面值 后面字段
[request addPostValue:@"0" forKey:@"command"];
[request addPostValue:@"rrrr" forKey:@"name"];
[request addPostValue:@"rrrr" forKey:@"psw"];
//开代理:asi不管是get post 同步,异步都只调用一套代理方法
[request setDelegate:self];
request.timeOutSeconds =30;
//请求方式
request.requestMethod =@"Post";
[request startAsynchronous];
}
-(IBAction)uploadRequest:(id)sender
{
//上传 post
//1.拿到数据
NSString *filePath =[[NSBundle mainBundle]pathForResource:@"123" ofType:@"txt"];
NSData *data =[[NSData alloc]initWithContentsOfFile:filePath];
ASIFormDataRequest *_request =[ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/%E4%B8%8A%E4%BC%A0%E6%9C%8D%E5%8A%A1%E5%99%A8/upload"]];
//追加数据 服务器没有规定上传资源字段时使用
[_request appendPostData:data];
//设置上传进度的代理
_request.uploadProgressDelegate =self;
_request.delegate =self;
[_request startAsynchronous];
}
- (IBAction)DownloadClick:(id)sender {
//下载
//http://localhost:8080/DownLoadServer/Xcode51.dmg.zip
//创建对象
_downLoadRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/DownLoadServer/Xcode51.dmg.zip"]];
//1.临时路径
NSString *tmpPath = [self getFilepath:@"tmp/xcode.zip"];
//2.完成路径
NSString *docuPath = [self getFilepath:@"Documents/xcode.zip"];
//3.目的路径 下载好保存的路径
_downLoadRequest.downloadDestinationPath =docuPath;
//4.文件下载好之前存的位置,(缓存)
_downLoadRequest.temporaryFileDownloadPath = tmpPath;
//5.开启下载进度代理 上传和下载的进度代理都会调用同一个代理
_downLoadRequest.downloadProgressDelegate = self;
//6.打开断点下载
_downLoadRequest.allowResumeForFileDownloads =YES;
/*
asi 断点下载好之前都会存储到开发者提供的临时路径文件下,当检测到下载完毕之后会把临时文件里面的下载好的东西搬到开发者提供的完成路径之下,开发者只需提供对应的路径 而断点下载的逻辑asi内部已经封装
*/
//异步下载
[_downLoadRequest startSynchronous];
}
-(NSString *)getFilepath:(NSString *)path{
return [NSHomeDirectory() stringByAppendingPathComponent:path];
}
- (IBAction)cancelBtnClick:(id)sender {
[_downLoadRequest cancel];
}
//检测网络
- (IBAction)ChangeWebclick:(id)sender {
//添加通知也可以检测网络
// 1.导入头文件
//2.初始化给一个有效的网络
Reachability *reach = [Reachability reachabilityWithHostName:@"http:www.baidu.com"];
//3.获取到当前检测的状态枚举
NetworkStatus status = [reach currentReachabilityStatus];
switch (status) {
case NotReachable:
{
NSLog(@"没网");
}
break;
case ReachableViaWiFi:
{
NSLog(@"付费网络/2g/3g/4g");
}
break;
case ReachableViaWWAN:
{
NSLog(@"WiFi");
}
break;
default:
break;
}
/*
//Apple NetworkStatus Constant Names.
NotReachable = kNotReachable,
ReachableViaWiFi = kReachableViaWiFi,
ReachableViaWWAN = kReachableViaWWAN
*/
}
- (void)requestStarted:(ASIHTTPRequest *)request
{
NSLog(@"开始响应");
}
- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders
{
NSLog(@"开始响应---头部%@",responseHeaders);
}
//请求结束返回数据
- (void)requestFinished:(ASIHTTPRequest *)request
{
//返回数据
// request.responseData 服务器返回的是数据源类型时调用
// request.responseString 服务器返回的字符数据类型调用
NSLog(@"-----%@",request.responseString);
// NSDictionary *dic =[NSJSONSerialization JSONObjectWithData:request.responseData options:nil error:nil];
}
- (void)setProgress:(float)newProgress
{
NSLog(@"当前进度为%.2f",newProgress);
}
//错误的(断网)
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSLog(@"出错");
}