단순 네트워크 패키지 요청

9911 단어 네트워크
//

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

좋은 웹페이지 즐겨찾기