iOS Development-HTTP protocol detailed

Source: Internet
Author: User

URL (Uniform Resource Locator)

What is a URL
The full name of the URL is uniform Resource Locator (Uniform Resource Locator)
With 1 URLs, you can find the only 1 resources on the Internet
URLs are the addresses and locations of resources, and each resource on the Internet has a unique URL

Basic format of URL = protocol://HOST address/path
For example:
Http://www.baidu.com
Http://www.oschina.net
protocol: Different protocols, representing different ways of resource finding and transfer of resources
host Address: The IP address (domain name) of the host where the resource resides
Path: The exact location of the resource in the host

Common protocols in URLs
HTTP超文本传输协议,访问的是远程的网络资源,格式是http://http协议是在网络开发中最常用的协议file访问的是本地计算机上的资源,格式是file://(不用加主机地址)mailto访问的是电子邮件地址,格式是mailto:FTP访问的是共享主机的文件资源,格式是ftp://
Introduction to the HTTP protocol

Access remote network resources, both mobile and PC, often using the HTTP protocol
Visit Baidu homepage: http://www.baidu.com
Get Sina Weibo data
Get mass reviews of group buy data

The role of the HTTP protocol
The full name of HTTP is Hypertext Transfer Protocol, Hypertext Transfer Protocol
Specify the data transfer format between the client and server
Enables clients and servers to communicate effectively with data

Simple and fast
Because the HTTP protocol is simple, the HTTP server's program size is small, so communication is fast

Flexible
HTTP allows the transfer of any type of data

HTTP 0.9 and 1.0 use non-persistent connections
This saves transfer time by restricting each connection to only one request, and after the server responds to a request from the client, the connection is immediately disconnected.

Basic communication Process for HTTP

To request data from the server using the HTTP protocol, you need to understand the HTTP communication process first

Complete HTTP communication can be divided into 2 major steps
Request: The client asks for data from the server
Response: The server returns the corresponding data for the client

Scenarios for sending HTTP requests in iOS

In iOS, the common scenario for sending HTTP requests is
Apple native (comes with)
Nsurlconnection: Simple to use, the oldest and most direct of a program
Nsurlsession:ios 7 new technology, features more powerful than nsurlconnection
Cfnetwork:nsurl* 's bottom, pure C language

Third-party frameworks
ASIHTTPRequest: Nickname "http Terminator", the function is extremely powerful, unfortunately has already stopped the update
Afnetworking: Easy to use, provides basic enough common functions, maintenance and users more
Mknetworkkit: Easy to use, from the hometown of San Diego India, maintenance and user less

Suggestions
In order to improve development efficiency, enterprise development is basically a third-party framework

Comparison of ASI and AFN architectures

Methods for sending HTTP requests

In the http/1.1 protocol, 8 ways to send HTTP requests are defined
GET, POST, OPTIONS, HEAD, PUT, DELETE, TRACE, CONNECT, PATCH
According to the original design of the HTTP protocol, different methods have different ways to operate the resources.
PUT: Increase
Delete: Delete
POST: Change
GET: Check
The most common is get and post (actually get and post can do and delete and change)

To interact with the server using GET and POST requests, you need to understand a concept first
Parameters
is the specific data that is passed to the server, such as the account number at login, password

Get and Post comparisons

The main differences between get and post are expressed in data passing
GET
After the request URL in the form of "to follow the parameters issued to the server, a number of parameters are separated by &, such as
Http://ww.test.com/login?username=123&pwd=234&type=JSON
Because the browser and server have a limit on the length of the URL, the parameters that accompany the URL are limited and usually cannot exceed 1KB

POST
All parameters sent to the server are placed in the request body
In theory, there is no limit to the amount of data that can be transmitted by post (depending on the processing power of the server)

Select the Get and post recommendations
If you want to pass large amounts of data, such as file uploads, you can only use POST requests
Get security is worse than post, if it contains confidential \ Sensitive information, it is recommended to use post
If you are only requesting data (data query), we recommend using get
If you are adding, modifying, or deleting data, it is recommended that you use post

HTTP Communication Process-Request

The HTTP protocol stipulates that a complete HTTP request sent by the client to the server contains the following content
Request Line: Contains the request method, the request resource path, the HTTP protocol version
Get/mjserver/resources/images/1.jpg http/1.1

Request Header: Contains information about the client's environment description, the host address requested by the client, and so on
host:192.168.1.105:8080//The server host address that the client wants to access
user-agent:mozilla/5.0 (Macintosh; Intel Mac OS X 10.9) firefox/30.0
Client-type, client-side software environment
accept:text/html, / //The type of data the client can receive
ACCEPT-LANGUAGE:ZH-CN//Client language environment
Accept-encoding:gzip//client-supported data compression formats

Request body: Specific data that the client sends to the server, such as file data

HTTP Communication Process-response

The client sends a request to the server and the server should respond by returning the data to the client
The HTTP protocol specifies that a complete HTTP response contains the following content
Status line: Contains the HTTP protocol version, status code, status English name
http/1.1 OK

Response header: Contains a description of the server, a description of the returned data
server:apache-coyote/1.1//Type of server
Content-type:image/jpeg//type of return data
content-length:56811//length of returned data
Date:mon, June 12:54:52 GMT//Response time

Entity content: Specific data that the server returns to the client, such as file data

HTTP Communication Process

Common response Status Codes

HTTP protocol Request Instance
 @interface httpviewcontroller ()@property(Weak,nonatomic)IboutletUitextfield *username;@property(Weak,nonatomic)IboutletUitextfield *pwd;-(ibaction) login;@end @implementation httpviewcontroller - (void) viewdidload{[SuperViewdidload];additional setup after loading the view, typically from a nib.}- (void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{[ Self. ViewEndediting:YES];} - (ibaction) Login {//1. User name    NSString*usernametext = Self. Username. Text;if(Usernametext. Length==0) {[Mbprogresshud showerror:@"Please enter user name"];return; }//2. Password    NSString*pwdtext = Self. PWD. Text;if(Pwdtext. Length==0) {[Mbprogresshud showerror:@"Please enter your password"];return; }/** Interface Documentation: Defines the server-side request interface 1> request path URL: Which path the client should request 2> request parameters: The data the client wants to send to the server * Username-username * pwd -Password 3> request result: The server will return something to the client */    //3. Send the user name and password to the server (take the HTTP protocol)    //Create a URL: request path    NSString*URLSTR = [NSStringstringwithformat:@"http://localhost:8080/server/login?username=%@&pwd=%@", Usernametext, Pwdtext];Nsurl*url = [NsurlURLWITHSTRING:URLSTR];//Create a request    nsurlrequest*request = [nsurlrequestRequestwithurl:url];//Send a sync request (send a request on the main thread)NSData *data = [nsurlconnectionSendsynchronousrequest:request Returningresponse:NilErrorNil];NSLog(@"%@", data);}@end

iOS Development-HTTP protocol detailed

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.