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