仿AFNetworking

#import "GJJHttpRequest.h"


@implementation GJJHttpRequest

- (void)dealloc {

    self.myFailedBlock = nil;

    self.mySuccessBlock = nil;

    

    [_httpRequest release];

    self.downloadData = nil;

    [super dealloc];

}

- (instancetype)init {

    if (self = [super init]) {

        //初始化 数据对象

        self.downloadData = [[[NSMutableData alloc] init] autorelease];

    }

    return self;

}

- (void)downloadDataWithUrl:(NSString *)urlStr success:(DownloadSuccessBlock)successBlock failed:(DownloadFailedBlock)failedBlock{

    if (_httpRequest) {

        [_httpRequest release];

        _httpRequest = nil;

    }

    //保存block 否则 block 就会随时释放

    self.mySuccessBlock = successBlock;

    self.myFailedBlock = failedBlock;

    

    

    //创建请求

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: urlStr]];

    //创建 下载连接

    _httpRequest = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    //一点创建 下载连接 就会启动一个线程去专门下载数据

}


#pragma mark - post


- (void)postDataWithUrl:(NSString *)url paramString:(NSString *)paramStr success:(DownloadSuccessBlock)successBlock failed:(DownloadFailedBlock)failedBlock {

    if (_httpRequest) {

        [_httpRequest release];

        _httpRequest = nil;

    }

    //保存block

    self.mySuccessBlock = successBlock;

    self.myFailedBlock = failedBlock;

    

    //创建可变请求

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];

    

    //设置请求方式

    request.HTTPMethod = @"POST";

    //设置请求头

    //设置请求的类型(请求体提交的数据的格式)

    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    //提交 参数(username=xiaohong&password=123456放在 请求体中进行提交)

    //把参数 放入 请求体

    // 参数拼接的字符串转化为 NSData

    NSData *data = [paramStr dataUsingEncoding:NSUTF8StringEncoding];

    request.HTTPBody = data;

    //设置请求体的长度

    [request setValue:[NSString stringWithFormat:@"%ld",data.length] forHTTPHeaderField:@"Content-Length"];

    //建立连接请求

    _httpRequest = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    

}


#pragma mark - NSURLConnectionDataDelegate

//客户端 接收到 服务器的响应

//服务器将要 发送数据

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    //清空旧数据

    [self.downloadData setLength:0];

}

//接收数据

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    [self.downloadData appendData:data];

}

//下载完成

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    

    //下载完成 之后 要回调block 解析数据

    if (self.mySuccessBlock) {

        //调用block 把下载数据传入

        self.mySuccessBlock(self.downloadData);

    }

}

//下载失败

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    if (self.myFailedBlock) {

        //回调

        self.myFailedBlock(error);

    }

}


@end


转载于:https://my.oschina.net/u/2433452/blog/497214

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值