IOS network basics and ios network Basics
Complete process of an HTTP request based on the iOS Network
Browser/application requests (requests include HTTP requests (GET, POST), address URLs, protocols (HTTP/1.1), request headers, and additional information)
--> Web server processing (page: Static html page, dynamic php/asp/jsp page) --> Browser/Application
Difference between GET and POST:
Both HTTP request methods
GET: GET data only. All parameters are included in the URL for simple data acquisition (Insecure)
POST: Get upload additional data. The request parameters are in the request header file and the encrypted information must be transmitted.
Use of network request open-source library AFNetWorking
// [Self testGetRequest]; // 1. GET request (html, json, xml) // [self testPostRequest]; // 2. POST request // [self testUploadFile]; // 3. upload a file // [self testDownloadFile]; // 4. download the object [self testMonitorNetworkStatus]; // 5. monitor network status // 6. asynchronous image function (instead of SDWebImage) // UIKit + AFNetworking. h //-(void) setImageWithURL :( NSURL *) url; // 7. some request special request headers // BAIDU_WISE_UID = wapp_1428425381699_466; // AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager]; // [manager. requestSerializer setValue: @ "wapp_1428385381699_466" forHTTPHeaderField: @ "BAIDU_WISE_UID"];
1. GET request
# Pragma mark-1. GET request-(void) testGetRequest {NSString * urlStr = @ "http://www.baidu.com"; // define AFHTTPRequest management object AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager]; // Note: by default, default data format: JSON, content-type: JSON, responseObjectd is the dictionary and array to be parsed. // No error Code is generated =-1016 // solution: Set the parser to the HTTP format, the downloaded file is NSData manager. responseSerializer = [AFHTTPResponseSerializer serializer]; [manager GET: urlStr parameters: nil success: ^ (AFHTTPRequestOperation * operation, id responseObject) {// responseObject important parameter --> contains the downloaded data NSString * str = [[NSString alloc] initWithData: responseObject encoding: NSUTF8StringEncoding]; NSLog (@ "str = % @", str);} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {NSLog (@ "error % @", error);}];}
2. POST request
# Pragma mark-2. POST request-(void) testPostRequest {// POST interface: http://quiet.local/testdir/login.php // parameter 1: @ "user": @ "quiet" // parameter 2: @ "password ": @ "123" NSString * urlString = @ "http://quiet.local/testdir/login.php"; AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager]; manager. requestSerializer = [AFHTTPRequestSerializer serializer]; // parameter 1: Input address // parameter 2: Input URL request parameter, format input dictionary // parameters --> to upload a dictionary @ {} [manager POST: urlString parameters: @ {@ "user": @ "quiet", @ "password ": @ "123"} success: ^ (AFHTTPRequestOperation * operation, id responseObject) {NSString * str = [[NSString alloc] initWithData: responseObject encoding: NSUTF8StringEncoding]; NSLog (@ "str = % @", str);} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {NSLog (@ "error % @", error);}];}
3. upload images
# Pragma mark-3. image Upload-(void) testUploadFile {// POST upload interface NSString * urlStr = @ "http://quiet.local/uploadtest/upload.php"; // Parameter Name: image: the parameter value is AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager]; manager. responseSerializer = [AFHTTPResponseSerializer serializer]; [manager POST: urlStr parameters: nil constructingBodyWithBlock: ^ (id <AFMultipartFormData> formData) {// implementation: the uploaded data is appended to the Request body. NSString * path = [[NSBundle mainBundle] pathForResource: @ "back2.jpg" ofType: nil]; // mimeType multi-purpose Internet Mail Extension type, each file has a type --> direct online search format [formData appendPartWithFileURL: [NSURL fileURLWithPath: path] name: @ "12222" fileName: @ "mddse.jpg" mimeType: @ "12222/jpeg" error: nil];} success: ^ (AFHTTPRequestOperation * operation, id responseObject) {NSString * str = [[NSString alloc] initWithData: responseObject encoding: NSUTF8StringEncoding]; NSLog (@ "str = % @", str);} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {NSLog (@ "error = % @", error);}];}
4. Download an object
# Pragma mark-4. download file-(void) testDownloadFile {NSString * urlString = @ "http://imgcache.qq.com/club/item/avatar/zip/7/i87/all.zip"; // create session manager object (default configuration) AFURLSessionManager * manager Region * manager = [[AFURLSessionManager alloc] region: [NSURLSessionConfiguration defaultSessionConfiguration]; // specify * task Region * task = [manager downloadTaskWithRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: urlString] progress: nil destination: ^ NSURL * (NSURL * targetPath, NSURLResponse * response) {// return the position where the file is saved NSString * path = [NSString stringWithFormat: @ "% @/Documents/all.zip", NSHomeDirectory ()]; NSLog (@ "path = % @", path); return [NSURL fileURLWithPath: path];} completionHandler: ^ (NSURLResponse * response, NSURL * filePath, NSError * error) {// NSLog (@ "downloaded") ;}]; // [task resume]; // start the task}
5. Monitor network status
# Pragma mark-5. monitoring Network Status-(void) testMonitorNetworkStatus {AFHTTPRequestOperationManager * manager = [[delealloc] initWithBaseURL: [NSURL URLWithString: @ "www.baidu.com"]; // [manager. reachabilityManager setReachabilityStatusChangeBlock: ^ (AFNetworkReachabilityStatus status) {// AFNetworkReachabilityStatusn statuses NSDictionary * dict =@{@ (unknown): @ "unknown", @ (unknown ): @ "inaccessible", @ (AFNetworkReachabilityStatusReachableViaWWAN): @ "GPRS", @ (AFNetworkReachabilityStatusReachableViaWiFi): @ "Wifi"}; NSLog (@ "status % @", dict [@ (status)]) ;}]; [manager. reachabilityManager startMonitoring]; // enable status monitoring}