Synchronous Send Request Module

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.