Development of IOS -- Network HTTP request

Posted by Brakanjan on Thu, 03 Mar 2022 17:47:39 +0100

I. overview

  • Several concepts in HTTP network request
  • Send HTTP request in IOS
  • HTTP request example

II. Several concepts in HTTP network request

2.1 URL

What is a URL

  • The full name of URL is Uniform Resource Locator
  • Through one URL, you can find the only resource on the Internet
  • URL is the address and location of resources. Each resource on the Internet has a unique URL

Basic format of URL

agreement://Host address / path
http://192.168.0.1/img/a.gif
  • Protocol: different protocols represent different resource search methods and resource transmission methods
  • Host address: the IP address (domain name) of the host where the resource is stored
  • Path: the specific location of the resource in the host

2.2 common protocols in ULR

HTTP

  • Hypertext Transfer Protocol accesses the most remote network resources in the format of http://
  • http protocol is the most commonly used protocol in network development

file

Access the resources on the local computer in the format of file: / / (without adding the host address)

mailto

The email address accessed is mailto:

FTP

Access the file resources of the shared host in the format of ftp://

2.3 introduction to HTTP protocol

Whether it is mobile client or PC, HTTP protocol is often used to access remote network resources

  • Visit Baidu homepage: https://www.baidu.com
  • Get Sina Weibo data
  • Get the group purchase data of public comments

reflection:

  • What format of data should the client send to the server? The server can understand
  • What format of data should the server return to the client? The client can understand
  • How can the two sides transmit data to communicate effectively?

Role of HTTP protocol

  • The full name of HTTP is Hypertext Transfer Protocol
  • Specify the data transmission format between the client and the server
  • So that the client and server can communicate data effectively

2.4 characteristics of HTTP protocol (why choose HTTP)

Simple and fast

Because the HTTP protocol is simple, the program scale of the HTTP server is small, so the communication speed is very fast

flexible

HTTP allows any type of data to be transmitted

HTTP0.9 and 1.0 use non persistent connections

Limit each connection to only one request. After the server makes corresponding response to the client's request, it will disconnect immediately. This method can save transmission time.

2.5 basic communication process of HTTP

The complete HTTP communication can be divided into two steps

  • Request: the client requests data from the server
  • Response: the server returns the data of the client response

III. send HTTP request in IOS

3.1 common schemes for sending HTTP requests in IOS

Apple Soundtrack

  • NSURLConnection: the oldest, most classic and most direct scheme with simple usage
  • NSURLSession: the new technology of IOS 7 has more powerful functions than NSURLConnection
  • CFNetwork: the bottom layer of NSURL, pure C language

Third party framework

  • ASIHttpRequest: nicknamed "HTTP terminator", it is extremely powerful. Unfortunately, it has long stopped updating
  • AFNetwroking: simple and easy to use, it provides basic and sufficient common functions, and has many maintenance and users
  • MKNetworkKit: easy to use, less maintenance and users

proposal

In order to improve the development efficiency, the third-party framework is basically used for enterprise development

3.2 common categories

NSURL

Request address

NSURLRequest

An NSURLRequest object represents a request and contains information

  • An NSURL object
  • Request method, request header and request body
  • request timeout
  • other

NSMutableURLRequest

Subclass of NSURLRequest

USURLConnection

  • Be responsible for sending requests and establishing the connection between the client and the server
  • Collect the data from the server and send the response to the server

3.3 steps for using nsurlconnection

The steps to send a request using NSURLConnection are simple

  • Create an NSURL object and set the request path
  • Pass in the NSURL object and set the request path
  • The incoming NSURL creates an NSURLRequest object and sets the request header and request body
  • Send an NSURLRequest using NSURLConnection

IV. example of HTTP request

4.1 code

#import "ViewController.h"
#import "MBProgressHUD+MJ.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *username;

@property (weak, nonatomic) IBOutlet UITextField *pwd;

@end

@implementation ViewController


- (IBAction)login:(UIButton *)sender
{
    //1 - user name
    NSString *usernameText=self.username.text;
    if (usernameText.length==0) {
        [MBProgressHUD showError:@"enter one user name"];
        return;
    }
    //2-password
    NSString  *pwdText=self.pwd.text;
    if (pwdText.length==0) {
        [MBProgressHUD showError:@"Please input a password"];
        return;
    }
    //3 - send user name and password to the server
    //NSLog(@ "send user name and password to server");
    NSString *urlStr=[NSString stringWithFormat:@"http://localhost:8080/MJServer/login?username=%@&pwd=%@",usernameText,pwdText];
    NSURL *url=[NSURL URLWithString:urlStr];
    //Create a request
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    //Send a synchronization request (send the request in the main thread)
    NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSLog(@"%@",data);
    
}
@end

4.2 results

client

Server side

Client information:
Request mode:GET, ip:0:0:0:0:0:0:0:1, environment:01-http%E7%BD%91%E7%BB%9C%E8%AF%B7%E6%B1%82/1 CFNetwork/1327.0.4 Darwin/21.3.0
 user name=123, password=123

Client printing

2022-03-03 21:46:50.819868+0800 01-http Network request[14274:138197] {length = 26, bytes = 0x7b227375 63636573 73223a22 e799bbe5 ... e68890e5 8a9f227d }

Topics: iOS xcode objective-c http