数据请求:NSURLConnection ,代理方法

//

//  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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值