In the process of learning iOS, staying in the UI control for a long time, is now gradually in contact with!!!!!! In this process, the small series learned some knowledge about network programming, and the feeling of the hair, here to share:
About the importance of network requests I don't want to say more!!! For mobile clients, the importance of the network is self-evident, common network requests have get, POST, today look at the two ways to implement network requests.
Understanding of the first part of the small series
One, two kinds of network request--post request and get request similarities and differences (here the small part only discusses the asynchronous link)
The semantics of get is to get the resource on the specified URL, add the data in the form of variable = value, after the URL to which the action points, and both use "?" Connections, using the "&" connection between each variable.
The syntax for post is to specify the resource append/Add resource, place the data on the data body, and pass it to the URL to which the action is directed, in the same way that the variable and value correspond.
1. The interface of the GET request contains the parameters section, and the parameters are passed as part of the URL and between the server address and the parameter. to the interval. The POST request separates the server address from the parameter, only the server address in the request interface, and the parameter is submitted to the backend server as part of the request.
2. Get request parameters appear in the interface, unsafe, and post requests are relatively secure, and all data is not visible to the user.
3.GET transmits a small amount of data, mainly by the URL length limit, and post can transfer data, upload files can only use post.
4. Although both get requests and post requests can be used to request and submit data, general get is used to request data from the background, and post is used to submit data to the background.
Ii. steps to network request
1. Determine address Nsurl
2. Build request Nsurlrequest
3. Establish and start the connection nsurlconnection
Wait for network processing ~ ~ ~
4. Handling network requests by proxy method
Compliance Protocol: Nsurlconnectiondatadelegate
Third, network Agent method
1. Receive the server response, the server to transmit data, the client to do receive preparation
2. The data transmitted by the receiving server may be executed multiple times
3. Receive data to complete, follow-up processing
4. Server request failed for many reasons (network environment, etc.)
5. Send data to the server, this method only applies to post, especially upload files
Four, Get method
In Nsurl, specify the parameters (if any, you can also specify no parameters), for example: http://www.baidu.com
Five, post method
define a variable urlmutablerequest
1) duration
[Request settimeoutinterval:2.0f];
2) Request method (default is Get)
[Request sethttpmethod:@ "POST"];
3) Data body
NSData *body = [string datausingencoding:nsutf8stringencoding];
[Request Sethttpbody:body];
Vi. synchronization Method-After a network request has to be completed, it can be followed, such as online banking login
See the __autoreleasing word in the method parameter and precede the defined object name with "&"
nsurlresponse *response = nil;
nserror *error = nil;
//The synchronization operation is not completed and the following code will not execute
NSData *data = [nsurlconnection sendsynchronousrequest:request returningresponse:&response Error:&error] ;
1> receiving data, indicating that it is working properly
2> did not receive data, but error is nil, indicating that it received empty data
the server usually does not respond to the request
3> error is not empty, indicating a request error
Vii. Async Methods
[nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *reponse, NSData *data, Nserror *error) {
//Processing after request is completed
}];
Async method does not wait for network request to end
Part two part of the demo
1 //1. Determine the address Nsurl2NSString *urlstring = [NSString stringWithFormat:@"http://xxxxxxx.php?xxxx=%@&xxxx=%@", xxx, xxx;3URLString =[URLString stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];4Nsurl *url =[Nsurl urlwithstring:urlstring];5 //2. Create a request Nsurlrequest6Nsurlrequest *request =[Nsurlrequest Requestwithurl:url];7 //3. Establish and initiate the connection Nsurlconnection8Nsurlconnection *conn = [Nsurlconnection connectionwithrequest:requestDelegate: self];9 //initiate connection, asynchronous connection requestTen [conn start]; One //Server notification readiness, ready for transit data ASelf.serverdata = [Nsmutabledata data];
Get Method
1 //1. Determine the address Nsurl2NSString *urlstring = [NSString stringWithFormat:@"http://XXXXX.php"];3Nsurl *url =[Nsurl urlwithstring:urlstring];4 //2. Create a request nsurlrequest (POST)5Nsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url];6 //1) Request Method7[Request Sethttpmethod:@"POST"];8 //2) Data body9 //because Datausingencoding has implemented transcoding.TenNSString *bodystr = [NSString stringWithFormat:@"username=%@&password=%@", UserName, pwd]; OneNSData *body =[Bodystr datausingencoding:nsutf8stringencoding]; A [Request Sethttpbody:body]; - //3. Establish and initiate the connection Nsurlconnection -Nsurlconnection *conn = [Nsurlconnection connectionwithrequest:requestDelegate: self]; the //initiate connection, asynchronous connection request - [conn start]; - //Server notification readiness, ready for transit data -Self.serverdata = [Nsmutabledata data];
Post Method
1- (void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{}2- (void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{}3- (void) Connectiondidfinishloading: (Nsurlconnection *) connection{}4- (void) Connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error{}5- (void) Connection: (Nsurlconnection *) connection Didsendbodydata: (Nsinteger) Byteswritten Totalbyteswritten: (Nsinteger) Totalbyteswritten Totalbytesexpectedtowrite: (nsinteger) Totalbytesexpectedtowrite6{}Network proxy methods
Small knitting Ability Limited, Hope Nira guidance, grateful!!!!!!
iOS network programming (Entry level)--Basics