Starting from this article, we introduce some techniques used in network development.
iOS network development, especially application development, using the HTTP protocol as the primary means of communication
Before writing the code, understand some of the content associated with the protocol, including:
The concept of URLs
HTTP and HTTPS
Content of the HTTP request and request object
Content of HTTP reply and request object
URL: A Uniform Resource identifier that can represent the path of a resource, which can be local, network.
URLs are a concise representation of the location and access methods available on the Internet, and are the addresses of standard resources on the Internet.
Each file on the Internet has a unique URL that contains information that indicates the location of the file and how the browser should handle it.
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/7C/B1/wKiom1bWVqijmTNGAABYnXG1MSE239.png " title= "screenshot 2016-03-02 a.m. 10.57.58.png" alt= "Wkiom1bwvqijmtngaabynxg1mse239.png"/>
Nsurl is the type of object used in objective-c to describe URL information, typically using a string to create
+ (Instancetype) urlwithstring: (NSString *) urlstring
Encoding issues for URLs
Special encoding and Chinese characters are not allowed in the URL, if required to convert to% encoded form
The following NSString classification method provides the processing of URL formats.
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/7C/AF/wKioL1bWWB-AqY1NAACyntsbotk827.png " title= "screenshot 2016-03-02 a.m. 11.02.08.png" alt= "Wkiol1bwwb-aqy1naacyntsbotk827.png"/>
HTTP: Hypertext Transfer Protocol (hypertext Transport Protocol) is a widely used network protocol on the Internet.
The purpose of the design is to provide an HTML page with a method for publishing and accepting, and later development, HTTP is not limited to manipulating HTML.
Current version: HTTP 1.1, default port: 80
The HTTP protocol is the standard TCP communication protocol of the C/S model, usually the server is the website, the client is the browser or the terminal application.
HTTPS: SSL layer is added under HTTP for secure HTTP data transfer
Default port: 443
From XCode7 onwards, the network request sent by the project must send an HTTPS request to allow HTTP requests to be sent:
In the Info.plist file, add the Nsapptransportsecurity field, type Nsdictionary
Add the Nsallowsarbitraryloads field to the dictionary, type Boolean, and the value Yes
As follows: This is a http://www.baidu.com/request packet capture data
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/7C/B1/wKiom1bWV_yjMA9MAAEDSDKmElg808.png " title= "screenshot 2016-03-02 a.m. 11.03.35.png" alt= "Wkiom1bwv_yjma9maaedsdkmelg808.png"/>
Contains three parts: request line, request header, request body
Request Line: Contains information such as request type, URL, protocol version, etc.
Request header: As the parameter of the request, with "field name": "Field value"
Multiple parameters can be specified, where the host field is required
Request body: Data body for HTTP
Typically, a request body is a form-encoded data for a Web browser
Typically, the request body is an XML or JSON-formatted data for mobile applications
HTTP request Type
get: Gets the content, and the data is added to the URL following the Variable=value form.
The transmitted data is not secure in the URL and the amount of data transferred is limited by the length of the URL.
POST: Add content, put data in the data body, data is not visible to users
You can transfer large amounts of data that can be used to upload files.
HEAD: request header, typically used to check for changes in the server's recent content
PUT: Send request (similar to post), the requested URL exists to modify the old version, does not exist to create.
Delete: Requests to delete resources, which can be used on some cloud services.
Other requests: OPTIONS, TRACE
Nsurlrequest is the Request object type, and the request object is usually created by a URL
+ (Instancetype) Requestwithurl: (Nsurl *) theURL
+ (Instancetype) Requestwithurl: (Nsurl *) theURL CachePolicy: (nsurlrequestcachepolicy) CachePolicy TimeoutInterval: ( Nstimeinterval) timeOutInterval
The second method, you can specify:
1) The requested cache policy (described later)
2) time-out on request to prevent infinite wait on request
Properties of the Nsurlrequest object:
URL Properties
@property (readonly, copy) Nsurl *url
HTTP request Type: (default is GET request)
@property (readonly, copy) NSString *httpmethod
HTTP request Body:
@property (readonly, copy) NSData *httpbody
HTTP request Header:
@property (readonly, copy) Nsdictionary *allhttpheaderfields
-(NSString *) Valueforhttpheaderfield: (NSString *) field
Nsmutableurlrequest Variable Request Object
Nsurlrequest is an immutable type whose subclasses are nsmutableurlrequest mutable types
Modifying the request header, the request body typically requires the use of this mutable type
@property (copy) NSString *httpmethod
-(void) SetValue: (NSString *) value Forhttpheaderfield: (NSString *) field
-(void) AddValue: (NSString *) value Forhttpheaderfield: (NSString *) field
Typically the requested parameters can be placed in:
Request Header
URL (GET request)
Request Body (POST request)
As the following GET request (drug search)
Interface Address: Http://apis.baidu.com/tngou/drug/search
Request Method: GET
Request parameters (header):
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/7C/B1/wKiom1bWWw2AKHqAAABb88r53sM298.png " title= "screenshot 2016-03-02 a.m. 11.16.38.png" width= "349" height= "0" border= "0" hspace= "0" vspace= "style=; height:83px; "alt=" Wkiom1bwww2akhqaaabb88r53sm298.png "/>
Request parameters (Urlparam):
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/7C/B0/wKioL1bWW6XBV0_LAADBXOIT3NA985.png " title= "screenshot 2016-03-02 a.m. 11.17.02.png" width= "550" height= "257" border= "0" hspace= "0" vspace= "0" style= "width:550px; height:257px; "alt=" Wkiol1bww6xbv0_laadbxoit3na985.png "/>
1) Request header (header parameter)
Usually place very important or security-related parameters, such as the Apikey in the example above is usually a string to describe an application ID
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/7C/B0/wKioL1bWXBfxB7PRAABzCRJFrbM490.png " title= "screenshot 2016-03-02 a.m. 11.19.05.png" alt= "Wkiol1bwxbfxb7praabzcrjfrbm490.png"/>
2) URL (GET request)
For get requests, some parameters are usually placed directly in the URL, in the form of the following parameters:
http://localhost/login.php ? username=aaa & password=123
Blue indicates a resource path.
Two symbols in red, which means that the urlparam,& is followed by a split of multiple parameters
The pink is the parameter, specified in Key=value form
3) Request body (POST request)
For post requests, the parameters are usually placed in the request body, and the benefit is that the parameters are not lost in the URL
The parameter format is usually:
1) The format described above is as follows:username=aaa&password=123
2) JSON 3) XML ...
The most cases are 1), such as:
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/7C/B0/wKioL1bWXMuw4mKTAABJqK4GaBA371.png " title= "screenshot 2016-03-02 a.m. 11.21.39.png" alt= "Wkiol1bwxmuw4mktaabjqk4gaba371.png"/>
As below, is a request for the packet capture data of the reply message
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7C/B1/wKiom1bWXN3geGZRAAEra5N3WsM790.png "title=" screen shot 2016-03-02 a.m. 11.24.26.png "alt=" Wkiom1bwxn3gegzraaera5n3wsm790.png "/>
The reply message includes three parts: status line, message header, reply data
Status line: Status code description Answer status
Message header: Similar to the request header, can contain a series of fields, such as the data type of the response MIME type, data length, etc.
Response data: Data body for HTTP
For web browsers are usually form code data;
For mobile applications It is usually json/xml data
HTTP response Status Code
The status code consists of 3 digits, and the first number represents a type:
1XX: Request received, continue processing
2XX: Success
3XX: Request Redirection
4XX: Client error, request cannot be processed by server
5XX: Server error, Server failed to process request
Common Status Codes:
200:ok, client request succeeded
400:bad request, client requests have syntax errors
401:unauthorized, unauthorized
403:forbidden, the server accepts the request but refuses to provide the service
404:not Found, the requested resource does not exist, URL error
503:server unavailable, the server is currently unable to process the request
MIME type
The message header of an HTTP reply contains a Content-type field that represents the type of data in the answer
Use the MIME (Multipurpose Internet Mail Extension) Multipurpose Internet Mail Extension
is an Internet standard that was first applied to e-mail and later applied to browsers and terminal reference programs
By this type, the server tells the browser or terminal application what type of data is being sent.
MIME types include: video, images, text, audio, applications, and so on, such as:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7C/B0/wKioL1bWXZryIejvAAG5vGIZyr8491.png "title=" screen shot 2016-03-02 a.m. 11.25.34.png "alt=" Wkiol1bwxzryiejvaag5vgizyr8491.png "/>
For mobile-side development, common: text/html text/plain text/json Application/json ...
A network request, you can typically get three of data:
Answer Object (nshttpurlresponse)
Error code (NSERROR, if an error occurs)
Response Data (NSData)
Usually for Json/xml
Nsurlresponse is the type of response message, which is usually returned by the framework's method when it is developed on the mobile side.
Its common properties are:
@property (readonly) long Long Expectedcontentlength@property (readonly, copy) NSString *suggestedfilename@property ( ReadOnly, copy) NSString *mimetype
Usually we are using the subclass Nshttpurlresponse of Nsurlresponse, which represents an HTTP reply message type
Message header @property (readonly, copy) nsdictionary *allheaderfields//status code: @property (readonly) Nsinteger statuscode+ ( NSString *) Localizedstringforstatuscode: (Nsinteger) StatusCode
This article is from the "Ammon" blog, make sure to keep this source http://annmeng.blog.51cto.com/3321237/1746659
iOS Web Development (1) What you need to know before you write your code