Use of network requests in iOS (GET requests vs. Post requests)

Source: Internet
Author: User

Get&post

Get and Post are the two most common HTTP methods for interacting with the server
GET
Semantics is the resource that gets the specified URL.
Add the data in the form Variable=value to the URL that the action points to, and both use "? Connect, use the & connection between the variables.
Seemingly unsafe, as 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 to add data to the resource of the specified URL.
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 the user.
You can transfer large amounts of data and upload files using post only.

Get Request Example

HTTP/localhost//login.php? username=zhangsan&password = Zhang
localhost: Local Host
Ogin.php: Resource Name
Semantics: the login.php Resource Acquisition (operation) from the local host specifies the login status of the user name and password.

Post Request Example
http://localhost/login.php
localhost: Local Host
/login.php: Resource Name
Semantics: Adds a user name and password to the local host's login.php resource and obtains the login status.
Hint: the user name and password are wrapped in the data body of the network request and are not visible in the URL.

Judging get&post requests in the browser
Because the POST request sends the data body to the server, a prompt window appears when the page is refreshed.
The GET request does not send the data body to the server, so there is no prompt.
From the nature of the request, get requests are more secure and more efficient than post requests.

Easily view the contents of a POST request with Firebug
When developing Web applications, it is important to develop a browser to track the return results of URLs.

Steps to send a request to the iOS network
1. Instantiating URLs (network resources)
2. Establish urlrequest based on URL (network request)
Default is GET request
For a POST request, the requested data body needs to be created.
3. Use URLConnection to send network requests (establish a connection).
4. Get Results

Nsurlconnection provides two static methods that can send network requests directly to the server in a synchronous or asynchronous manner.
Sync Request
SendSynchronousRequest:returningResponse:error:
Asynchronous Request:
SendAsynchronousRequest:queue:completionHandler:

Binary data stream of network transmission

In the process of network requests, the process of accepting data is actually
Nsurlconnectiondatadelegate, common proxy methods include:

The server begins to return data, ready to work -(void) Connection:didreceiveresponse: Receives the data returned by the server (this method is called multiple times)-(void  ) Connection:didreceivedata: Data received, final processing of data -(void) connectiondidfinishloading: Network connection error -(void) Connection:didfailwitherror: Let nsurlconnectiondatadelegate execute asynchronously [connection   Setdelegatequeue:[nsoperationqueue  alloc] init]

Advantages and disadvantages of using proxies
Disadvantages:
Agent method is many, more scattered.
To process a request, you need to write code in many places.
is not conducive to logical implementation, code writing, debugging, maintenance, and extension.
Especially when there are multiple requests, it becomes very cumbersome.

Advantages:
When downloading larger files, the role of the agent will be irreplaceable.

Writing data to a file

//1. Open FileNsfilehandle *FP =[Nsfilehandle FileHandleForWritingAtPath:self.downloadPath];//2. If the file does not existif(fp = =Nil) {//write data directly to a file[Data WriteToFile:self.down.downloadPath Atomically:yes]; }Else{//if the file already exists, move to the end of the file[FP seektoendoffile];//Append Data File[FP writedata:data];//Close File[FP closefile];}

Use of network requests in iOS (GET requests vs. Post requests)

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.