Network ProtocolA collection of rules, standards, or conventions established for data exchange in a computer network.URL: Is the URL. http://www.hcios.com/archives/1547The URL is the address and location of the resource. Every resource on the Internet has a unique url.url basic format = protocol://Host address/path. HTTP protocol: HTTP is an object-oriented protocol belonging to the application layer, because of its simple and fast way, it is suitable for distributed hypermedia information System, the full name is Hypertext Transfer Protocol (Hypertext Transfer Protocol), and it belongs to the application layer of the hierarchy model above. HTTPS protocol: HTTPS (full name: Hyper Text Transfer Protocol over secure Socket Layer), is a security-targeted HTTP channel, simply speaking is the security version of HTTP. That is, the SSL layer is added under HTTP, the security base of HTTPS is SSL, so the detailed content of encryption requires SSL (Secure Sockets Layer).SSL is a security protocol proposed by Netscape company , which constructs a secure channel between the browser and the Web server for data transmission, and SSL runs on top of the TCP/IP layer and provides encrypted data channel for the application under the application layer. the main differences between HTTPS and HTTP are the following four points:
- The HTTPS protocol requires a certificate to be applied to the CA, and the general free certificate is very small and requires a fee.
- HTTP is a Hypertext Transfer Protocol, the information is plaintext transmission, HTTPS is a secure SSL encryption transport protocol.
- HTTP and HTTPS use a completely different connection, the same port, the former is 80, the latter is 443.
- The HTTP connection is simple and stateless; The HTTPS protocol is a network protocol built by the SSL+HTTP protocol for encrypted transmission and authentication, which is more secure than the HTTP protocol.
Anyway, we just need to remember that HTTPS is more secure than HTTP. However, after iOS9.0, it is necessary to use HTTPS instead of HTTP for in-app network access, so if your interface is HTTP, some simple configuration is required.Network-based applications determine that the data link is not only the client (application) This ring, but also need to have a server (data source) This ring, when we need to send a request to the server to obtain data.The request method. In fact, the request is not only get, post these two kinds. There are: GET, POST, OPTIONS, HEAD, PUT, DELETE, TRACE, CONNECT, patch eight kinds of more. And the most common is get, Post.There are two types of requests that we can use: a GET request, a POST request. A GET request is a direct concatenation of information behind a URL, which is used more in real development, and a POST request is a translation of some of the more important information into a binary stream. In real development, post requests are common in verifying that the user name password is correct, uploading data to the server, and so on. Note whether a GET request or a POST request is determined by the background, the programmer can tell through the interface documentation whether the current request is get or post. The request is not limited to get and post, but also delete and put, except that a GET request can use Nsurlrequest, and other requests must use Nsmutableurlrequset. Explicitly specify what the current HttpMethod is requesting.
1 #pragmaMark-Synchronous GET request2 //1. Create a URL3Nsurl *url = [Nsurl Urlwithstring:get_url];//get_url is a URL link for a macro definition4 //2. Create a specific request based on the URL (using a cache policy)5Nsurlrequest *request = [Nsurlrequest requestwithurl:url cachepolicy:nsurlrequestreloadignoringlocalcachedata timeOutInterval: -];6 //3. Linked Servers7 //Parameter 1: Request Object8 //Parameter 2: Store information for some network requests (typically package) now generally write nil9 //Parameter 3: Error messageTenNSData *data =[nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil]; One //4. JSON Data parsing ANsdictionary *resultdic =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error:nil]; -NSLog (@"%@", resultdic);
Synchronizing GET Requests
1 #pragmaMark-Synchronous Post method2 //Create URL3Nsurl *url =[Nsurl Urlwithstring:post_url];4 //Create a network request [the stitching setting for the content must be variable]5Nsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url];6 7 //Set Body8 //Create a link string (this content is labeled in the future development of the interface document)9NSString *datastring =Post_body;Ten //encode the link string (This step can not be forgotten) OneNSData *postdata =[datastring datausingencoding:nsutf8stringencoding]; A - //set request format as POST request (Letters must all be uppercase) -[Request Sethttpmethod:@"POST"]; the //Set the request body (body) - [Request Sethttpbody:postdata]; - - //3. Linked Servers +NSData *data =[nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil]; - //4. JSON Data parsing +Nsdictionary *dic =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error:nil]; ANSLog (@"%@", DIC);
synchronizing a POST request
1 #pragmaMark-Asynchronous GET request2 //1. Create a URL3Nsurl *url =[Nsurl Urlwithstring:get_url];4 //2. Create Session Object5Nsurlsession *session = [Nsurlsession sharedsession];//Single Case6 //3. Create Task Request Tasks7 //Nsurlsessiontask is based on the task to complete the related events8 //Nsurlsessiontask, all the tasks are put in this.9Nsurlsessiondatatask *task = [Session Datataskwithurl:url completionhandler:^ (NSData * _nullable data, NSURLResponse * _N Ullable Response, Nserror *_nullable Error) {Ten //4. Parse the relevant data One if(Error = =Nil) { ANsdictionary *dic =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error:nil]; -NSLog (@"%@", DIC); - } the }]; - //5. Core part: Start task [This step is easy to forget] - //Cause: The task of the Nsurlsessiontask instance is in a suspended state, and if it does not start, it will not walk the implementation content in block -[Task resume];Asynchronous GET Request
#pragmaMark-Asynchronous POST request//1. Create a URLNsurl *url =[Nsurl Urlwithstring:post_url]; //2. Create a requestNsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url]; //Core Settings BodyNSString *bodystr =Post_body; NSData*postdata =[Bodystr datausingencoding:nsutf8stringencoding]; [Request Sethttpmethod:@"POST"]; [Request Sethttpbody:postdata]; //3. Create Session ObjectNsurlsession *session =[Nsurlsession sharedsession]; //4. Create a taskNsurlsessiondatatask *task = [Session datataskwithrequest:request completionhandler:^ (NSData * _Nullable data, Nsurlresponse * _nullable response, Nserror *_nullable Error) { //5 Parsing DataNsdictionary *dic =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error:nil]; NSLog (@"%@", DIC); }]; //6. Start a task[Task resume];Asynchronous POST Request
Synchronous and asynchronous differences: Synchronous Request network data, time is instant access to data, each time is immediately from the network to obtain data. Asynchronous can be read from previously cached data, which makes it much quicker to asynchronously load data than it does synchronously.
HTTP and HTTPS protocols