Simple Network request/GET request/POST request, network get request post
# Pragma mark-Simple Network request-(IBAction) simpleRequest :( id) sender {// 1. construct url nsurl * url = [NSURL URLWithString: @ "http://news-at.zhihu.com/api/3/news/latest"]; // 2. construct Request NSURLRequest * request = [NSURLRequest requestWithURL: url]; // 3. construct a Session NSURLSession * session = [NSURLSession sharedSession]; // 4. construct the task/*** task ** @ data returned by param data * @ param response Header * @ param error message **/NSURLSessionDataTask * task = [session dataTaskWithRequest: request completionHandler: ^ (NSData * data, NSURLResponse * response, NSError * error) {NSLog (@ "response: % @", response); if (error = nil) {/* NSString * dataStr = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; NSLog (@ "data: % @", dataStr ); * // json --> data is created using a JSON object. // NSJSONSerialization * jsonData = [NSJSONSerialization dataWithJSONObject: <# (id) #> options: <# (NSJSONWritingOptions) #> error: <# (NSError * _ autoreleasing *) #>]/* options: 1. read reading NSJSONReadingMutableContainers to generate a variable object. If this option is not set, the default value is NSJSONReadingMutableLeaves to generate a variable string MutableString (iOS7 + bug) NSJSONReadingAllowFragments allows the outermost layer of json data not a dictionary or array. writing NSJSONWritingPrettyPrinted to generate json data is formatted with line breaks and high readability * // data --> Create json NSJSONSerialization * jsonData = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: nil]; NSLog (@ "json: % @", jsonData); // JSONKit open-source framework} else {// handle corresponding errors}]; // 5. send request [task resume] ;}# pragma mark-GET request-(IBAction) getRequest :( id) sender {// GET request, you can also send information to the server, there are also parameters (Weibo user name, user ID) // 1. construct the URL, the parameter is directly spliced after the url Connection NSURL * url = [NSURL URLWithString: @ "http://news-at.zhihu.com/api/3/news/4602734"]; // 2. construct a Request // Save the get request header in the request // NSURLRequest * Request = [NSURLRequest requestWithURL: url]; // parameter // (1) url // (2) Cache Policy // (3) Timeout time. After 120 seconds, the request is abandoned. // NSURLRequest * request = [NSURLRequest requestWithURL: url cachePolicy: required timeoutInterval: 120]; // NSURLRequest is unchangeable and cannot dynamically add request header information // variable object NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url]; // (1) set the request method [request setHTTPMethod: @ "GET"]; // (2) Timeout value [request setTimeoutInterval: 120]; // (3) Cache Policy [request setCachePolicy: NSURLRequestReturnCacheDataElseLoad]; // (4) set other content in the request Header // [request setValue: <# (NSString *) #> forHTTPHeaderField: <# (NSString *) #>]; // [request addValue: <# (NSString *) #> forHTTPHeaderField: <# (NSString *) #>]; // [request setAllHTTPHeaderFields: <# (NSDictionary *) #>]; [request setValue: @ "gzip" forHTTPHeaderField: @ "Accept-Encoding"]; // inform the service that the returned data must be compressed. // 3. construct a Session NSURLSession * session = [NSURLSession sharedSession]; // 4. task NSURLSessionDataTask * task = [session dataTaskWithRequest: request completionHandler: ^ (NSData * data, NSURLResponse * response, NSError * error) {if (error = nil) {NSString * dataStr = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; NSLog (@ "data: % @", dataStr) ;}}]; // 5. [task resume] ;}# pragma mark-POST request-(IBAction) postRequest :( id) sender {// POST request // 1. construct url nsurl * url = [NSURL URLWithString: @ "https://api.weibo.com/2/statuses/update.json"]; // 2. construct Request NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url]; // (1) set to post request [Request setHTTPMethod: @ "POST"]; // (2) timeout [request setTimeoutInterval: 60]; // (3) set the request header // [request setAllHTTPHeaderFields: nil]; // (4) set the Request body // send Weibo // The Request body must contain at least two parameters // access_token to specify the user's token (which will be described later) // status Weibo body NSString * bodyStr = @ "access_token = XXXXX & status = content we want to send"; NSData * bodyData = [bodyStr dataUsingEncoding: NSUTF8StringEncoding]; [request setHTTPBody: bodyData]; // 3. construct a Session NSURLSession * session = [NSURLSession sharedSession]; // 4. task NSURLSessionDataTask * task = [session dataTaskWithRequest: request completionHandler: ^ (NSData * data, NSURLResponse * response, NSError * error) {NSLog (@ "response: % @", response);}]; // 5. [task resume];}