IOS network with multi-threaded--4. Synchronous Post Mode network request

Source: Internet
Author: User

Through the Post request method, the synchronization obtains the network data, once sends the synchronization request, the program stops the user interaction, until the server returns the data

Add the test code to the Viewdidload function within the viewcontroller.m file

1- (void) Viewdidload {2 [Super Viewdidload];3     //additional setup after loading the view, typically from a nib.4     5     //1. Create a Web site object that specifies the URL of the requested data6Nsurl *url = [Nsurl urlwithstring:@"http://www.baidu.com"];7     //2. Create a network Request object via the URL .8     //parameter 1: request access Path9     //Parameter 2: Cache protocolTen     //Parameter 3: Network request time-out OneNsmutableurlrequest *request = [[Nsmutableurlrequest alloc] Initwithurl:url cachepolicy: Nsurlrequestuseprotocolcachepolicy timeOutInterval:Ten]; A     //3. Set the network communication mode to post, the default is get -[Request Sethttpmethod:@"POST"]; -     //4. Setting parameters for network requests theNSString *str =@"type=focus-c"; -     //5. Converting the request parameters to binary data -NSData *data =[str datausingencoding:nsutf8stringencoding]; - [Request Sethttpbody:data]; +     //6. Using Network connection objects for network communication, a network connection is created after the network Connection object is created successfully.  -NSData *received =[nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil]; +     //7. Convert the returned data into a string and output ANSString *STR1 =[[NSString alloc] initwithdata:received encoding:nsutf8stringencoding]; atNSLog (@">>>>>>>%@", str1); -}

After execution, the data can be obtained based on the specified URL.

IOS network with multi-threaded--4. Synchronous Post Mode network request

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.