1.NSURLConnection代理下载设置在本地的身骑着白马MP3
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 // Do any additional setup after loading the view, typically from a nib. 4 5 self.data = [NSMutableData data]; 6 7 NSString * urlString = @"http://localhost/身骑白马.mp3"; 8 9 //如果接口中有中文 10 urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 11 12 NSURL * url = [NSURL URLWithString:urlString]; 13 14 NSURLRequest * request = [NSURLRequest requestWithURL:url]; 15 16 NSURLConnection * connect = [NSURLConnection connectionWithRequest:request delegate:self]; 17 18 //开启网络连接 19 [connect start]; 20 } 21 22 //服务器返回响应信息 23 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 24 25 NSLog(@"%@",response); 26 27 } 28 29 //接受数据(多次调用) 30 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 31 32 [self.data appendData:data]; 33 NSLog(@"%@",data); 34 35 } 36 37 //请求完成 38 - (void)connectionDidFinishLoading:(NSURLConnection *)connection{ 39 40 [self.data writeToFile:@"/Users/mac/Desktop/身骑白马.mp3" atomically:YES]; 41 NSLog(@"网络请求完成"); 42 43 }
2.GET请求
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //使用get请求,获取接口 NSString * String = @"http://localhost/login.php"; //拼接参数 NSString * urlString = [NSString stringWithFormat:@"%@?username=xubaoaichiyu&password=123456",String]; //如果有中文进行转码 urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL * url = [NSURL URLWithString:urlString]; NSURLRequest * request = [[NSURLRequest alloc]initWithURL:url cachePolicy:0 timeoutInterval:15]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { NSString * string = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@",string); }]; } @end
3.POST请求
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //使用post请求 //获取接口 NSString * string = @"http://localhost/login.php"; //中文转码 string = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL * url = [NSURL URLWithString:string]; //可变请求 NSMutableURLRequest * requst = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:0 timeoutInterval:15]; //设置传输方式 requst.HTTPMethod = @"POST"; NSString * bodyString = [NSString stringWithFormat:@"username=xubaoaichiyu&password=123456"]; //设置请求体 requst.HTTPBody = [bodyString dataUsingEncoding:NSUTF8StringEncoding]; [NSURLConnection sendAsynchronousRequest:requst queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { NSString * string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@",string); }]; }