IOS network analysis-(four get & post), ios network analysis get
The default network request is get.
There are many types of network requests: GET query POST change PUT add DELETE HEAD
In normal development, get and post are mainly used.
Get to get data (get user information)
The get request has no length limit. The actual length limit is made by the browser. The length limit is generally 2 k.
Get requests are cached and get algorithms have idempotence.
Get http: // localhost/login. php? Username = xubaoaichiyu & password = 123456
Request Parameters are exposed in the url
Get request parameter format:
? Followed by Request Parameters
Parameter Name = parameter value
& Connect two parameters
Post add and modify data (upload or modify user information)
THE post request is not cached.
Http: // localhost/login. php
There is no length limit for post. Generally, the length is less than 2 MB.
Post request parameters are not exposed and sensitive information is not exposed.
The request header and request body boby (the post parameter is placed in the Request body)
The get code is as follows:
/// ViewController. m // CX-get /// Created by ma c on 16/3/17. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // use the get request to obtain the NSString * String = @" http: // localhost/login. php "; // The splicing parameter NSString * urlString = [NSString stringWithFormat: @" % @? Username = xubaoaichiyu & password = 123456 ", String]; // urlString = [urlString encoding: NSUTF8StringEncoding]; NSURL * url = [NSURL URLWithString: urlString]; NSURLRequest * request = [[NSURLRequest alloc] initWithURL: url cachePolicy: 0 timeoutInterval: 15]; [NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^ (NSURLResponse * _ Nullable response, NSData * _ Nullable data, NSError * _ Nullable connectionError) {NSString * string = [[NSString alloc] initWithData: data encoding: encoding]; NSLog (@ "% @", string) ;}@ end
Post:
/// ViewController. m // CX-post /// Created by ma c on 16/3/17. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // use the post request // obtain the NSString * string = @" http: // localhost/login. php "; // Chinese transcoding string = [string stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; NSURL * url = [NSURL URLWithString: string]; // variable request NSMutableURLRequest * requst = [[NSMutableURLRequest alloc] initWithURL: url cachePolicy: 0 timeoutInterval: 15]; // set the transmission mode requst. HTTPMethod = @ "POST"; NSString * bodyString = [NSString stringWithFormat: @ "username = xubaoaichiyu & password = 123456"]; // set the Request body requst. HTTPBody = [bodyString dataUsingEncoding: Unknown]; [NSURLConnection failed: requst queue: [queue mainQueue] completionHandler: ^ (NSURLResponse * _ Nullable response, NSData * _ Nullable data, NSError * _ Nullable connectionError) {NSString * string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; NSLog (@ "% @", string) ;}];}