There are two kinds of synchronous loading, one is the direct load mode and the other is how to generate the page after the standard synchronization request is sent.
The realization of the direct way is requestwithurl:url, and this kind of scheme is basically very few people use.
The way to send a standard synchronization request is through the Sendsynchronousrequest function.
Our standard synchronous request module is implemented through the proxy, just paste the code, the use of the method of the steps.
ChttpSendSynchronousRequest.h file
#import <Foundation/Foundation.h>@interface chttpSendSynchronousRequest : NSObject- (void)sendHttpRequest:(NSString*)sendString : (NSInteger *)resultCode : (NSString **)recvData;@end
CHTTPSENDSYNCHRONOUSREQUEST.M file
- (void) Sendhttprequest: (NSString*) Sendstring: (Nsinteger*) ResultCode: (NSString* *) recvdata{if(Nil= = ServerURL)//|| (nil = = sendstring)){*resultcode =-1; RecvData =Nil;return; }Nsurl*servernusurl = [NsurlUrlwithstring:serverurl];NSLog(@"%@", sendstring);Nserror*error =Nil; Nsmutableurlrequest *request = [Nsmutableurlrequest requestwithurl:servernusurl Cachepolicy:nsurlrequestreloadignoringcachedata timeOutInterval: -]; [Request sethttpmethod:@"POST"]; [Request setvalue:@"Text.html"forhttpheaderfield:@"Content-type"];NSString* SendData = sendstring; NSData *postdata = [SendData datausingencoding:nsutf8stringencoding allowlossyconversion:YES];NSString*postlength = [NSStringstringwithformat:@"%d", [postdata length]]; [Request Setvalue:postlength forhttpheaderfield:@"Content-length"]; Nsinputstream *is = [Nsinputstream inputstreamwithdata:postdata]; [Request Sethttpbodystream:is]; NSData *nsrecvdata = [nsurlconnectionSendsynchronousrequest:request Returningresponse:Nilerror:&error];if(Nil! = Error) {*resultcode =-1; RecvData =Nil;return; }NSString* Recvstrdata = [[NSStringAlloc] Initwithdata:nsrecvdata encoding:nsutf8stringencoding];if(Nil= = Recvstrdata) {*resultcode =-1; RecvData =Nil;return; }NSLog(@"recvstrdata[%@]", Recvstrdata); *recvdata = Recvstrdata; *resultcode =0;return;}@end
Example of direct synchronous loading of image code via Requestwithurl:url:
- (void) Loadimagefromurl: (Nsurl*) URL {nsurlrequest* Request = [nsurlrequestRequestwithurl:url Cachepolicy:nsurlrequestuseprotocolcachepolicy timeOutInterval:10.0]; [_data setLength:0]; _connection = [[nsurlconnectionAlloc] Initwithrequest:request Delegate: Self];} - (void) Connection: (nsurlconnection*) theconnection Didreceivedata: (NSData *) incrementaldata {NSData * da = [[NSData alloc] Initwithdata:incrementaldat A]; [_data Appenddata:da]; Loginfo (@"succeeded%d byte received", [_data length]);NSString* FileName2 = [ Selffilepath:@"Image23.jpg"]; [Da writetofile:filename2 atomically:YES];} - (void) Connection: (nsurlconnection*) Theconnection Didfailwitherror: (Nserror*) error{//When the request fails, the relevant operation;[_data setLength:0];} - (void) Connectiondidfinishloading: (nsurlconnection*) theconnection {loginfo (@"succeeded%d byte received", [_data length]);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Send request module synchronously