[IOS syncs the templates for Get and post data]

Source: Internet
Author: User

1. Synchronous get (networked detection)

+(BOOL) checkconnect{//Initializing requestsNsmutableurlrequest *request =[[Nsmutableurlrequest alloc] init]; NSString*url=[nsstring stringWithFormat:@"http://%@/zjut_java_ios/checkconnect.jsp", HostIP]; //Set URL[Request Seturl:[nsurl Urlwithstring:url]; //Setting the HTTP method//[Request sethttpmethod:@ "GET"]; //send a sync request, and the returndata here is the return data.NSData *returndata =[nsurlconnection sendsynchronousrequest:request returningresponse:nil E        Rror:nil]; NSString*returnstr =[[NSString alloc] Initwithdata:returndata encoding:nsutf8stringencoding]; NSLog (@"Detecting networked JSP return results:%@", RETURNSTR); if([Returnstr isequaltostring:@"Connect"]) {NSLog (@"Networking detection Success!"); return true; }Else{NSLog (@"Networking detection failed!"); return false; }}

2. Synchronous post (Register new user, with MD5 encryption, do not need to ignore the MD5 function)

//Add a new user+ (BOOL) Insertnewuser: (NSString *) email VALUEOFPSW: (NSString *) psw{NSString*md5psw=[self MD5:PSW]; //the parameters for the post submission are in the following format://parameter 1 name = parameter 1 data & parameter 2 name = parameter 2 data & Parameter 3 name = parameter 3 data &NSString *post = [NSString stringWithFormat:@"email=%@&psw=%@", EMAIL,MD5PSW]; //The nssrring format parameter conversion format for Nsdata,post commits must be nsdata data. NSData *postdata =[post datausingencoding:nsutf8stringencoding Allowlossyconversion:yes]; //calculate the length of the post submission dataNSString *postlength = [NSString stringWithFormat:@"%lu", (unsignedLong) [postdata length]]; //define NsmutableurlrequestNsmutableurlrequest *request =[[Nsmutableurlrequest alloc] init]; //set submission Destination URL[Request Seturl:[nsurl urlwithstring:[nsstring stringWithFormat:@"http://%@/zjut_java_ios/insertnewuser.jsp", HostIP]]; //Set the submission mode to POST[Request Sethttpmethod:@"POST"]; //Set Http-header:content-type//This is set to application/x-www-form-urlencoded, if set to other, such as Text/html;charset=utf-8, or text/html, etc., will be wrong. I don't know what the reason is. [Request SetValue:@"application/x-www-form-urlencoded"Forhttpheaderfield:@"Content-type"]; //Set Http-header:content-length[Request Setvalue:postlength Forhttpheaderfield:@"Content-length"]; //set up content that requires post submission[Request Sethttpbody:postdata]; //definitionnshttpurlresponse* Urlresponse =Nil; Nserror*error =[[Nserror alloc] init]; //Synchronous Commit: The post commits and waits for the return value (synchronous), the return value is the NSData type. NSData *responsedata = [nsurlconnection sendsynchronousrequest:request returningresponse:&urlresponse Error: &ERROR]; //Convert the return value of the NSData type to the NSString typeNSString *result =[[NSString alloc] Initwithdata:responsedata encoding:nsutf8stringencoding]; NSLog (@"Register JSP return result:%@", result); if([Result isequaltostring:@"true"]) {NSLog (@"Registration Successful"); return true; }Else{NSLog (@"Registration Failed"); return false; }}+ (NSString *) MD5: (NSString *) str{Const Char*cstr =[str utf8string]; unsignedCharresult[ -]; CC_MD5 (CStr, strlen (CSTR), result); //The MD5 callNsmutablestring *mstr =[nsmutablestring stringwithcapacity:cc_md5_digest_length];  for(intI=0; i<cc_md5_digest_length; i++) {[MSTR AppendFormat:@"%02x", Result[i]]; }    returnMSTR;}

[IOS syncs the templates for Get and post data]

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.