As long as it belongs to the category of mobile development, network request must be a major play. Everyone is familiar with object-C network request, but there are not many familiar ones, because third-party libraries are more convenient to use. For example, AFNetworking, which we are familiar with, is used in Swift. There are some well-packaged web request libraries, but I’m not talking about third-party ones today. Instead, I’m talking about native request methods. Here’s the code:

import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let getBtn = UIButton(type: .System) getBtn.frame = CGRectMake(50, 100, 100, 100) getBtn.setTitle("GET", forState: .Normal) getBtn.setTitleColor(UIColor.blackColor(), forState: .Normal) getBtn.addTarget(self, action: #selector(self.GETACtion), forControlEvents: .TouchUpInside) self.view.addSubview(getBtn) let postBtn = UIButton(type: .System) postBtn.frame = CGRectMake(self.view.frame.size.width - 150, 100, 100, 100) postBtn.setTitle("POST", forState: .Normal) postBtn.setTitleColor(UIColor.blackColor(), forState: .Normal) postBtn.addTarget(self, action: #selector(self.POSTACtion), forControlEvents: .touchupinside) self.view.addSubView (postBtn)} /** If you look below you will find that GET is almost the same as POST. Here you see in the GET method parameters can be put in the request body, and the difference between most search to all think the GET parameter on the request link, this statement is wrong, GET parameters can completely in the request body, not because the GET connection length is limited, a limit is not the HTTP protocol, but the server and the browser, So at first glance, GET and POST are almost the same, but there are essential differences between them in HTTPMethod. If the dictionary is empty, please do not use request.HTTPMethod = "XXX", otherwise you will GET an error. So, as you can see, when the dictionary is empty, there is no difference between POST and GET. */ * GET request */ func GETACtion() {// request URL let URL :NSURL! = NSURL(string: "http://iappfree.candou.com:8080/free/applications/limited") let request:NSMutableURLRequest = NSMutableURLRequest(URL: url) let list = NSMutableArray() var paramDic = [String: String]() if paramDic. Count > 0 {// set request.HTTPMethod = "GET" For subDic in paramDic {let tmpStr = "\(subDic.0)=\(subDic.1)" list.addobject (tmpStr)} / / use & joining together into a string dictionary all let paramStr = list.com ponentsJoinedByString (" & ") / / UTF8 transcoding, Prevent illegal sites associated with Chinese character symbol let paraData = paramStr. DataUsingEncoding (NSUTF8StringEncoding) / / set the request body request. HTTPBody = paraData} / / the default session configuration let config. = NSURLSessionConfiguration defaultSessionConfiguration () let the session = NSURLSession(configuration: Config) / / a request let dataTask = session. DataTaskWithRequest (request) {(data, the response, the error) in / / let the STR: String! = String(data: data! , encoding: NSUTF8StringEncoding) // print(" STR :\(STR)") // convert Json let jsonData:NSDictionary = try! NSJSONSerialization.JSONObjectWithData(data! , options: .MutableContainers) as! NSDictionary print(jsonData)} datatask.resume ()} /** POST request */ func POSTACtion() {// request URL let URL :NSURL! = NSURL(string: "http://iappfree.candou.com:8080/free/applications/limited") let request:NSMutableURLRequest = NSMutableURLRequest(URL: url) let list = NSMutableArray() var paramDic = [String: String]() if paramDic. Count > 0 {// Set request to POST request.HTTPMethod = "POST" For subDic in paramDic {let tmpStr = "\(subDic.0)=\(subDic.1)" list.addobject (tmpStr)} / / use & joining together into a string dictionary all let paramStr = list.com ponentsJoinedByString (" & ") / / UTF8 transcoding, Prevent illegal sites associated with Chinese character symbol let paraData = paramStr. DataUsingEncoding (NSUTF8StringEncoding) / / set the request body request. HTTPBody = paraData} / / the default session configuration let config. = NSURLSessionConfiguration defaultSessionConfiguration () let the session = NSURLSession(configuration: Config) / / a request let dataTask = session. DataTaskWithRequest (request) {(data, the response, the error) in / / let the STR: String! = String(data: data! , encoding: NSUTF8StringEncoding) // print(" STR :\(STR)") // convert Json let jsonData:NSDictionary = try! NSJSONSerialization.JSONObjectWithData(data! , options: .MutableContainers) as! NSDictionary print(jsonData)} datatask.resume ()} Override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }Copy the code

Download address