IOS: Introduction to Network programming

Source: Internet
Author: User

1. Network programming: HTTP protocol, XML data protocol, JSON data protocol:

There are four ways to transfer data: Get mode, post mode, synchronous request mode, asynchronous request mode.  Description: Synchronous and asynchronous requests are made by either a GET or post method when creating a linked object and creating a request object; So what is the difference between synchronous/asynchronous request and Get/post mode? The difference is that in connection processing this step, the Get/post way they all use the same proxy nsurlconnectiondatadelegate method to transfer data, and the synchronous/asynchronous request method does not require the use of proxies.  
 2. Implementation steps:• First step: Determine Address--url nsurl (address)• Step two: Establish request nsurlrequest (request, GET request use)/nsmutableurlrequest (variable request, POST request use)• Step three: Start connecting nsurlconnection (connect)

Waiting for network processing ...

• Fourth step: Connect complete, Process results  3.<NSURLConnectionDataDelegate> method, get and post when sending data, use a proxy during the connection process, and use the proxy because the result of the request returned is processed!

The server begins to return data

-(void) Connection:didreceiveresponse:

When the data returned by the server is received, this method is called multiple times

-(void) Connection:didreceivedata:

Data received, the final processing of data

-(void) connectiondidfinishloading:

Network connection Error

-(void) Connection:didfailwitherror:

Send data to the server, POST requests Use this method

-(void) Connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:

unsigned long enconding = cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000)

4, GET request step--1. Address 2. Request 3. Connection 4. Start up

(1) Define the URL:

NSString *urlstr = @ "http://127.0.0.1/login.php?username=admin&password=123";Nsurl *url = [Nsurl urlwithstring:[urlstr Stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]];(2) define Reauest:nsurlrequest *request = [Nsurlrequest requestwithurl:url]; (3) Define connection:nsurlconnection *conn = [[Nsurlconnection alloc]initwithrequest:request delegate:self]; (4) Start connection:[conn start];  5. Post request step--1. Address 2. Request 3. Connection 4. Start Up(1) Define URLNSString *urlstr = @ "http://xxx.com/login.php"; (2) Define request:nsmutableurlrequest *request = [Nsmutableurlrequest requestwithurl:url];//The maximum time to wait for the server to respond[request settimeoutinterval:5.0];//Setting Request method, case- insensitive[request sethttpmethod:@ "POST"];NSString *bodystring = [NSString stringwithformat:@ "username=%@&password=%@", _usernametext.text,_ Passwordtext.text];//Generating the request body data and encodingNSData *body = [bodystring datausingencoding:nsutf8stringencoding];//Setting the HTTP request data Body[request sethttpbody:body]; (3) Define connection:nsurlconnection *conn = [[Nsurlconnection alloc]initwithrequest:request delegate:self]; (4) Start connection:[conn start];  6. Synchronous and asynchronous requests (the first two steps are the same as Get or post): 1. Address 2. Request 3. Using synchronous/Asynchronous requestsNsurlconnection provides two static methods that can call nsurlrequest directly or asynchronously without obtaining data through nsurlconnectiondatadelegate• Sync requests:

Sendsynchronousrequest:request Returningresponse:&response Error:&error

• Asynchronous Request:

Sendasynchronousrequest:request Queue:[nsoperationqueue Mainqueue] completionhandler:^ (NSURLResponse *response, NSData *data, Nserror *error)

• The server transmits the returned data, parsing the type of encoding it will use:

unsigned long encoding = cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000);

IOS: Introduction to Network programming

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.