//
// ViewController.m
// 3、异步请求
//
// Created by yongma00 on 15/12/10.
// Copyright © 2015年 yongma. All rights reserved.
//
#import "ViewController.h"
/*
我们可以使用NSMutableData来下载小文件
如果我们在这里面去使用NSMutableData来下载大文件的时候,明显不合理
*/
@interface ViewController ()<NSURLConnectionDataDelegate>{
NSMutableData *dataM;
}
- (IBAction)btnPressed:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
dataM=[[NSMutableData alloc]init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnPressed:(id)sender {
// //1、生成请求的url
// NSString *str=@"http://192.168.0.100:8080/MJServer/login";
// NSURL *url=[NSURL URLWithString:str];
// NSData *postData=[@"username=123&pwd=123&type=JSON" dataUsingEncoding:NSUTF8StringEncoding];
// //2、根据请求生成对应的Request
// NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
// [request setHTTPMethod:@"POST"];
// [request setHTTPBody:postData];
//1、生成请求的url
NSString *str= @"http://192.168.0.107:8080/MJServer/resources/videos/videos.zip";
NSURL *url=[NSURL URLWithString:str];
//2、根据请求生成对应的Request
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
//3、创建一个连接并且指定代理人为自己
NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];
}
//每次请求将要发送之前调用的方法,如果在这个方法里面不返回请求,那么就没有请求
- (nullable NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(nullable NSURLResponse *)response{
NSLog(@"%@---%@",request,response);
return request;
}
//接收到数据的时候调用
//会调用很多次
//数据是一点点过来的。
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
// NSLog(@"%@",data);
// [dataM appendData:data];
}
//请求完成的时候调用这个方法
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"结束");
}
@end