There are several ways to submit data to the server in Network programming: iOS sync request, asynchronous request, get request, POST request
1, the synchronization request can request data from the Internet, once the synchronization request is sent, the program will stop the user interaction until the server returns the data to complete before the next operation,
2, the asynchronous request does not block the main thread, but will establish a new thread to operate, after the user makes an asynchronous request, still can work on the UI, the program can continue to run
3, get request, the parameters are written directly on the access path. Simple operation, but easy to be seen by the outside, security is not high, address up to 255 bytes;
4, POST request, put the parameters inside the body. Post request operation is relatively complex, need to separate parameters and addresses, but high security, parameters placed inside the body, not easily captured. Next, borrow the code to demonstrate the implementation of the four request methods: The first is to use: Get request mode
1-(void) Loginbyget2 {3 //1. Create a Nsurl object to design the address of the connection4NSString *strurl =[nsstring stringWithFormat:@"http://127.0.0.1/usermanager/login.php?username=%@&password=%@", Self.usernametextfield.text,self.passwordtextfield.text];5Nsurl *url =[Nsurl Urlwithstring:strurl];6 7 //2. Create a Nsurlrequest Request object8Nsurlrequest *request =[Nsurlrequest Requestwithurl:url];9 /**Ten * Nsurlreqest If it is immutable, you cannot set the time-out period. One */ A //3. Create a Nsurlconnection Connection object -Nsurlconnection *connection = [Nsurlconnection connectionwithrequest:requestDelegate: self]; - the //4. Send a request to the server - [connection start]; -}
The second is the use of: Post request mode
1-(void) Loginbypost2 {3 //1. Create a Nsurl object to design the address of the connection4NSString *strurl =@"http://127.0.0.1/userManager/login.php";5Nsurl *url =[Nsurl Urlwithstring:strurl];6 7 //2. Create a Nsurlrequest Request object8Nsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url];9[Request Settimeoutinterval:5.0];//set the wait time for the connection timeout;Ten[Request Sethttpmethod:@"Post"];//Set the request mode to post One //set the data for the post band ANSString *strbody = [NSString stringWithFormat:@"submit=1&username=%@&password=%@", Self.usernametextfield.text,self.passwordtextfield.text]; -NSData *databody =[Strbody datausingencoding:nsutf8stringencoding]; - //carrying data through the protocol header the [Request Sethttpbody:databody]; - - //3. Create a Nsurlconnection Connection object -Nsurlconnection *conn = [Nsurlconnection connectionwithrequest:requestDelegate: self]; + - //send a request to the server + [conn start]; A at}
The third is the use of: Synchronous request mode
1-(void) Loginbypost2 {3 //1. Create a Nsurl object to design the address of the connection4NSString *strurl =@"http://127.0.0.1/userManager/login.php";5Nsurl *url =[Nsurl Urlwithstring:strurl];6 //2. Create a Nsurlrequest Request object7Nsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url];8[Request Settimeoutinterval:5.0];//Timeout period9[Request Sethttpmethod:@"Post"];//set up to use the Post methodTen //set the data for the post band OneNSString *strbody = [NSString stringWithFormat:@"submit=1&username=%@&password=%@", Self.usernametextfield.text,self.passwordtextfield.text]; ANSData *databody =[Strbody datausingencoding:nsutf8stringencoding]; - //carrying data through the protocol header - [Request Sethttpbody:databody]; the - //3. Create a Nsurlconnection Connection object -Nsurlconnection *conn = [Nsurlconnection connectionwithrequest:requestDelegate: self]; - //4. Send a request to the server + [conn start]; - + //Clean up data ANSData *data = [@""datausingencoding:nsutf8stringencoding]; at [Self.alldatas Setdata:data]; - -}
When you use Nsurlconnection to connect objects, you also need to implement the <NSURLConnectionDataDelegate> proxy method
1 #pragmaMark-nsurlconnection Proxy method2 //the data returned by the server may be executed many times, because if the data is large, it will be sent in a sub-packet3-(void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) Data4 {5 //stitching all data segments to append the newly received data segments6 [Self.alldatas Appenddata:data];7 }8 //error handling during connection9-(void) Connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) ErrorTen { OneNSLog (@"Error reason:%@", error); A } - //the data is received and the final processing is processed. --(void) Connectiondidfinishloading: (Nsurlconnection *) Connection the { - //This encoding is not recognized by the system when transferring files that contain Chinese characters -UnsignedLongencoding =cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000); - //according to encoding this encoding, will receive the data segment to output +NSString *request =[[NSString Alloc]initwithdata:self.alldatas encoding:encoding]; -NSLog (@"%@", request); +}
In the POST request mode, if you want to see the progress of the data transfer, you can print the output by implementing the following methods.
1 // The progress of the data transfer is returned as a post, limited to the post mode 2 -(void) connection: (Nsurlconnection *) connection didsendbodydata: (Nsinteger) Byteswritten Totalbyteswritten: (Nsinteger) Totalbyteswritten totalbytesexpectedtowrite: (Nsinteger) TotalBytesExpectedToWrite 3{4 NSLog (@ "bytes:%ld,totalbytes:%ld,totalbytesexpected:% LD", byteswritten,totalbyteswritten,totalbytesexpectedtowrite); 5 }
Knowledge Point supplement: GET request and POST request mode
GET
- The semantics of get is to get the resource on the specified URL
- Add the data in the form of Variable=value, followed by the URL to which the action points, and both use "?" Connection, using "&" connection between each variable
- Unsafe because the data is placed in the requested URL during the transfer
- The amount of data transferred is small, mainly because of the URL length limit
POST
- The semantics of post is "append/Add" data to the specified resource
- Place the data in the data body and pass it to the URL that the action points to, in the same way that the variable and the value correspond
- All data is not visible to users
- can transfer a lot of data , upload files can only use post
Four Steps for network access
- First step: Determine address--url
- Step two: Create a request
- Step three: Start the connection
Waiting for network processing ...
- Fourth Step: Connection complete, processing result
Ios_ Network Programming