简单网络请求封装

//

//  HTTPRequset.h

//  请求类分装

//

//  Created by qianfeng on 15-7-26.

//  Copyright (c) 2015年 qq. All rights reserved.

//

//通过代理和block实现函数回调,可选其一

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

@class HTTPRequset;

@protocol HTTPRequsetDelegate <NSObject>



@optional

/**

    进度的回调

 */

- (void)requestDidProgress:(CGFloat)progress;

//请求完成的回调

- (void)requestDidfinish:(HTTPRequset *)httprequest;

/**

    请求完成的回调

 */

- (void)requestDidError:(NSError *)error;

@end



/**

    请求类

 */

@interface HTTPRequset : NSObject

/**

    网络请求返回数据源

 */

@property(nonatomic,strong) NSData * responseData;

/**

    字符串形式

 */

@property(nonatomic,copy ) NSString * reponseString;

/**

    代理

 */

@property(nonatomic,assign)id<HTTPRequsetDelegate>delegate;

/**

 使用block实现回调下载进程

 */

@property(nonatomic,copy) void(^requestDidProgressBlock)(CGFloat progress);

/**

 使用block实现回调对象

 */

@property(nonatomic,copy) void(^requestDidFinishBlock)(HTTPRequset * request);

/**

 使用block实现回调错误信息

 */

@property(nonatomic,copy) void(^requestDidErrorBlock)(NSError * error);



/**

 构造方法

 */

- (id)initWithUrl:(NSURL *)url;

/**

    开始网络请求

 */

- (void)startRequest;

@end
//

//  HTTPRequset.m

//  请求类分装

//

//  Created by qianfeng on 15-7-26.

//  Copyright (c) 2015年 qq. All rights reserved.

//



#import "HTTPRequset.h"



@interface HTTPRequset ()<NSURLConnectionDataDelegate,NSURLConnectionDelegate>

{

    NSURL * _url;

    //保存下载数据

    NSMutableData * _downloadData;

    

    //总大小

    long long _fileSizeBytes;

}

@end



@implementation HTTPRequset

- (id)initWithUrl:(NSURL *)url

{

    self = [super init];

    if (self) {

        

        _url = url;

        _downloadData = [NSMutableData data];

    }

    return self;

}

- (void)startRequest

{

    NSURLRequest * request = [NSURLRequest requestWithURL:_url];

    [NSURLConnection connectionWithRequest:request delegate:self];

}

#pragma mark - 代理方法

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

{

    //清空数据

    _downloadData.length = 0;

    //获取文件总大小

    _fileSizeBytes = [response expectedContentLength];

}



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

{

    //追加

    [_downloadData appendData:data];

    //计算进度

    float progress = (float) _downloadData.length / _fileSizeBytes;

    if ([self.delegate respondsToSelector:@selector(requestDidProgress:)]) {

        [self.delegate requestDidProgress:progress];

    }

    self.requestDidProgressBlock(progress);

    

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    //请求取消

    [connection cancel];

    

    //

    _responseData = _downloadData;

    _reponseString = [[NSString alloc]initWithData:_downloadData encoding:NSUTF8StringEncoding];

    self.requestDidFinishBlock(self);

    if ([self.delegate respondsToSelector:@selector(requestDidfinish:)]) {

        [self.delegate requestDidfinish:self];

    }

    

    

    

}

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

{

    //请求取消

    [connection cancel];

    self.requestDidErrorBlock(error);

    if ([self.delegate respondsToSelector:@selector(requestDidError:)]) {

        [self.delegate requestDidError:error];

    }

    

}







@end

 

你可能感兴趣的:(网络)