Network request Encapsulation
/// Asihttprequest + request. h // cloudshopping /// created by sixiaobo on 14-7-9. // copyright (c) 2014 COM. uni2uni. all rights reserved. // # import "asiformdatarequest. H "# import" asidownloadcache. H "// downloaddata is the returned data. If an error occurs, the error is put into error; otherwise, the error is nil. You can use the error parameter // to determine whether the typedef void (^ hybresultblock) is incorrect) (nsdata * downloaddata, nserror * error); // hybrequesttype enumeration is used to specify the request type typedef ns_enum (nsuinteger, hybreques TType) {ktypepost = 1 <1, // POST request ktypeget = 1 <2/GET request}; @ interface hybhttprequest: asiformdatarequest // request callback block, this block will be called back if it succeeds or fails. The error parameter is used to determine whether it is successful @ property (nonatomic, copy) hybresultblock resultblock; @ property (nonatomic, strong) nsmutabledata * downloaddata; // The downloaded data @ property (nonatomic, assign) hybrequesttype requesttype; /// // asynchronous request Method /////////// //////////////*! * @ Brief uses the POST Request Method by default * @ Param path: Network request prefix parameter * @ Param Params uses dictionary storage, the callback block * @ return of * @ Param completion completed in the request URL is spliced internally. hybhttprequest object */-(ID) initwithpath :( nsstring *) path Params :( nsdictionary *) is returned *) params completion :( hybresultblock) completion;-(ID) initwithpath :( nsstring *) path Params :( nsdictionary *) Params completion :( hybresultblock) completion iscache :( bool) iscache // whether to cache, the default value of the POST request is no isrefresh :( bool) isrefresh; // whether to refresh the cache-(ID) initwithpath :( nsstring *) path Params :( nsdictionary *) Params requesttype :( hybrequesttype) requesttype completion :( hybresultblock) completion;-(ID) completion :( nsstring *) path Params :( nsdictionary *) Params requesttype :( hybrequesttype) requesttype completion :( hybresultblock) completion iscache :( bool) iscache // indicates whether the request is cached. The default value of post is no; isrefresh :( bool) isrefresh; // indicates whether to refresh the cache-(ID) initwithpath :( nsstring *) path requesttype :( hybrequesttype) requesttype completion :( hybresultblock) completion;-(ID) initwithpath :( nsstring *) path requesttype :( hybrequesttype) requesttype completion :( hybresultblock) completion iscache :( bool) iscache // whether to cache, the default value of post requests is no; isrefresh :( bool) isrefresh; // whether to refresh the cache // the request must be a POST request. The request parameters must be converted to JSON-format data-(ID) initwithpath :( nsstring *) path postbody :( nsmutabledata *) postbodyjsondata completion :( hybresultblock) completion; // The request parameter must be a POST request and be converted to JSON-format data-(ID) initwithpath :( nsstring *) path postbody :( nsmutabledata *) postbodyjsondata completion :( hybresultblock) completion iscache :( bool) iscache // whether to cache, POST request no by default; isrefresh :( bool) isrefresh; // whether to refresh the cache // cancel the request-(void) cancelrequest; @ end
/// Asihttprequest + request. M // cloudshopping /// created by sixiaobo on 14-7-9. // copyright (c) 2014 COM. uni2uni. all rights reserved. // # import "hybhttprequest. H "# import" asiformdatarequest. H "# import" nsstring + common. H "# import" hybhttprequestmanager. H "# import" nsstring + encrypt. H "# import" nsfilemanager + file. H "@ interface hybhttprequest () @ property (nonatomic, assign) bool iscache; @ property (nonatom IC, assign) bool isrefresh; @ property (nonatomic, copy) nsstring * filename; @ end @ implementation hybhttprequest-(ID) initwithpath :( nsstring *) path Params :( nsdictionary *) params requesttype :( response) requesttype completion :( hybresultblock) completion {return [self defined: path Params: Params requesttype: requesttype completion: Completion iscache: requesttype = ktypeget isrefresh: No];} -(ID) in Itwithpath :( nsstring *) path Params :( nsdictionary *) Params completion :( hybresultblock) completion iscache :( bool) iscache // whether to cache, POST request is no isrefresh by default :( bool) isrefresh {// whether to refresh the cache return [self initwithpath: path Params: Params requesttype: ktypepost completion: Completion iscache: iscache isrefresh: isrefresh];}-(ID) initwithpath :( nsstring *) path Params :( nsdictionary *) Params requesttype :( hybrequesttype) req Uesttype completion :( hybresultblock) completion iscache :( bool) iscache isrefresh :( bool) isrefresh {If (Self = [Super initwithurl: [nsurl urlwithstring: path]) {self. iscache = iscache; self. delegate = self; self. resultblock = [Completion copy]; self. downloaddata = [[nsmutabledata alloc] init]; self. requesttype = requesttype; self. filename = path; self. isrefresh = isrefresh; If (self. requesttype = kt Ypeget) {[self setrequestmethod: @ "get"]; // set permanently stored locally} else if (self. requesttype = ktypepost) {[self setrequestmethod: @ "Post"]; [self addrequestheader: @ "Content-Type" value: @ "application/JSON"]; If (Params) {self. filename = [nsstring stringwithformat: @ "% @? ", Self. filename]; for (nsstring * key in Params. allkeys) {[self addpostvalue: [Params objectforkey: Key] forkey: Key]; self. filename = [nsstring stringwithformat: @ "% @%@=%@", self. filename, key, [Params objectforkey: Key] ;}}// if it is cached // and it is not refreshed if (self. isrefresh = No & self. iscache & [[nsfilemanager defaultmanager] isfileexists: [self cachepath]) {If (! [[Nsfilemanager defaultmanager] isfile: [self cachepath] Timeout: 12*60*60]) {nsdata * Data = [[nsdata alloc] initwithcontentsoffile: [self cachepath]; self. downloaddata = [data mutablecopy]; If (data. length! = 0) {self. resultblock (data, nil); return self ;}} [[hybhttprequestmanager sharedrequestmanager] addrequest: Self withkey: Self. filename. MD5]; [self startasynchronous];} return self;}-(ID) initwithpath :( nsstring *) path Params :( nsdictionary *) Params completion :( hybresultblock) completion {return [self initwithpath: path Params: Params requesttype: ktypepost completion: Completion];}-(ID) initwithpath :( Nsstring *) path completion :( hybresultblock) completion {return [self initwithpath: path Params: Nil completion: Completion];}-(ID) initwithpath :( nsstring *) path requesttype :( hybrequesttype) requesttype completion :( hybresultblock) completion {return [self initwithpath: path Params: Nil requesttype: requesttype completion: Completion iscache: requesttype = ktypeget isrefresh: No];}-(ID) initwithpath :( nsstr Ing *) path requesttype :( hybrequesttype) requesttype completion :( hybresultblock) completion iscache :( bool) iscache // whether to cache, the default value of post requests is no; isrefresh :( bool) isrefresh {// whether to refresh the cache return [self initwithpath: path Params: Nil requesttype: requesttype completion: Completion iscache: iscache isrefresh: isrefresh];} // must be a POST request, request parameters to be converted to JSON format data-(ID) initwithpath :( nsstring *) path postbody :( nsmutabledata *) postbodyjsond ATA completion :( hybresultblock) completion {return [self initwithpath: path postbody: postbodyjsondata completion: Completion iscache: No isrefresh: Yes];}-(ID) initwithpath :( nsstring *) path postbody :( nsmutabledata *) postbodyjsondata completion :( hybresultblock) completion iscache :( bool) iscache // whether to cache, the default value of post requests is no; isrefresh :( bool) isrefresh {// whether to refresh the cache if (Self = [Super initwithurl: [nsurl urlwithstring: P Ath]) {self. delegate = self; self. resultblock = [Completion copy]; self. downloaddata = [[nsmutabledata alloc] init]; self. requesttype = ktypepost; self. iscache = iscache; self. isrefresh = isrefresh; self. filename = path; If (postbodyjsondata. length! = 0) {nsstring * STR = [[nsstring alloc] initwithdata: postbodyjsondata encoding: nsutf8stringencoding]; self. filename = [nsstring stringwithformat: @ "% @", self. filename, STR];} If (self. requesttype = ktypepost) {[self setrequestmethod: @ "Post"]; [self addrequestheader: @ "Content-Type" value: @ "application/JSON"]; [self addrequestheader: @ "accept" value: @ "application/JSON"]; [self setpostbody: postbod Yjsondata];} // if it is a cache // and does not refresh the cache if (self. isrefresh = No & self. iscache & [[nsfilemanager defaultmanager] isfileexists: [self cachepath]) {If (! [[Nsfilemanager defaultmanager] isfile: [self cachepath] Timeout: 12*60*60]) {nsdata * Data = [[nsdata alloc] initwithcontentsoffile: [self cachepath]; self. downloaddata = [data mutablecopy]; If (data. length! = 0) {self. resultblock (data, nil); return self ;}} [[hybhttprequestmanager sharedrequestmanager] addrequest: Self withkey: Self. filename. MD5]; [self startasynchronous];} return self;}-(void) cancelrequest {[self cleardelegatesandcancel]; return ;}# Pragma mark-asihttprequestdelegate-(void) request :( asihttprequest *) Request didreceiveresponseheaders :( nsdictionary *) responseheaders {[self. downloaddata setlength: 0]; return;}-(void) requestfinished :( asihttprequest *) Request {[[hybhttprequestmanager sharedrequestmanager] removerequestwithkey: Self. filename. MD5]; If (self. resultblock) {[self. downloaddata writetofile: [self cachepath] atomically: Yes]; self. resultblock (self. downloaddata, nil);} return;}-(void) requestfailed :( asihttprequest *) Request {[[hybhttprequestmanager sharedrequestmanager] removerequestwithkey: Self. filename. MD5]; If (self. resultblock) {[self cleardelegatesandcancel]; self. resultblock (self. downloaddata, self. error);} return;}-(void) Request :( asihttprequest *) Request didreceivedata :( nsdata *) Data {[self. downloaddata appenddata: Data]; return ;}# Pragma mark-obtain the cache path-(nsstring *) cachepath {return [nsstring stringwithformat: @ "% @/% @", [nsstring cachepath], self. filename. MD5] ;}@ end
//// Hybhttprequestmanager. h // cloudshopping /// created by sixiaobo on 14-7-9. // copyright (c) 2014 COM. uni2uni. all rights reserved. // # import <Foundation/Foundation. h> # import "asihttprequest. H "# import" asidownloadcache. H "/*! * @ Brief manage the life cycle of the asihttprequest object * @ author huangyibiao */@ interface Duration: nsobject @ property (nonatomic, strong) asidownloadcache * downloadcache; + (optional *) sharedrequestmanager; /*! * @ Brief Add the asihttprequest object and use the request object managed by hybhttprequestmanager * @ Param urlstringkey to use the absolute URL as the key */-(void) addrequest :( ID) Request withkey :( nsstring *) urlstringkey ;/*! * @ Brief clears the proxy of the request object based on the specified key, cancels the request, and removes the hybhttpreuest object * @ Param urlstringkey absolute URL */-(void) removerequestwithkey :( nsstring *) urlstringkey; /*! * @ Brief be cautious here. Once called, all request objects will be removed */-(void) removeallrequest ;/*! * @ Brief cancels all requests and removes */-(void) cancelallrequestandremove; @ end
//// Hybhttprequestmanager. M // cloudshopping /// created by sixiaobo on 14-7-9. // copyright (c) 2014 COM. uni2uni. all rights reserved. // # import "hybhttprequestmanager. H "# import" hybhttprequest. H "@ interface hybhttprequestmanager () @ property (nonatomic, strong) implements * requestdict; @ end @ implementation hybhttprequestmanager + (hybhttprequestmanager *) sharedrequestmanager {static hybhttpre Questmanager * sharedmanager = nil; static dispatch_once_t oncetoken; dispatch_once (& oncetoken, ^ {If (! Sharedmanager) {sharedmanager = [[self class] alloc] init] ;}}); Return sharedmanager ;}- (ID) Init {If (Self = [Super init]) {self. requestdict = [[nsmutabledictionary alloc] init];} return self;}-(void) addrequest :( ID) Request withkey :( nsstring *) urlstringkey {nsstring * error = [nsstring stringwithformat: @ "error in % s, key is nil", _ function _]; nsassert (urlstringkey! = Nil, error); [self. requestdict setobject: Request forkey: urlstringkey]; return;}-(void) removerequestwithkey :( nsstring *) urlstringkey {nsstring * error = [nsstring stringwithformat: @ "error in % s, key is nil ", _ function _]; nsassert (urlstringkey! = Nil, error); Id request = [self. requestdict objectforkey: urlstringkey]; If ([Request iskindofclass: [asihttprequest class]) {[Request cleardelegatesandcancel];} else {nsurlconnection * connection = (nsurlconnection *) request; [connection cancel];} [self. requestdict removeobjectforkey: urlstringkey]; return;}-(void) removeallrequest {[self. requestdict removeallobjects]; return;}-(void) cancelallrequestandremove {for (asihttprequest * request in self. requestdict. allvalues) {[Request cleardelegatesandcancel];} [self. requestdict removeallobjects]; return ;}@ end
Network request Encapsulation