IOS-GET request details-GET sets the Request Header

Source: Internet
Author: User

IOS-GET request details-GET sets the Request Header

A rare remark is a very detailed native GET network request operation, which is recorded as obsessive-compulsive disorder and shared with you... Also used for backup Replication

-(Void) getResult {_ MB = [MBProgressHUD showHUDAddedTo: self. navigationController. view animated: YES]; // interface path NSString * path = @ "http://a.apix.cn/apixlife/phone/phone"; // path-+ parameter NSString * pathWithPhoneNum = [NSString stringWithFormat: @ "% @? Phone = % @ ", path, _ phoneNumFD. text]; // Chinese encoded NSString * urlPath = [pathWithPhoneNum character: [NSCharacterSet URLQueryAllowedCharacterSet]; // url nsurl * phoneURL = [NSURL URLWithString: urlPath]; // request object NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: phoneURL]; // request method [request setHTTPMethod: @ "GET"]; // request Header [request setValue: @ "92b5787ecd1720.b718 Activities "activities: @" apix-key "]; // network configuration NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; // Network session NSURLSession * session = [NSURLSession sessionWithConfiguration: configuration]; // task NSURLSessionDataTask * sessionTask = [session dataTaskWithRequest: request completionHandler: ^ (NSData * _ Nullable data, NSURLResponse * _ Nullable response, NSError * _ Nullable error) {// return to the main thread to update the UI-> cancel the mask dispatch_async (dispatch_get_main_queue (), ^ {[_ MB hide: YES];}); if (error) {NSLog (@ "request failed... % @ ", error); // prompt that the user request failed! UIAlertController * AV = [UIAlertController alertControllerWithTitle: @ "prompt" message: @ "sorry, server error. Please try again later... "preferredStyle: Regular]; [AV addAction: [UIAlertAction actionWithTitle: @" OK "style: UIAlertActionStyleDefault handler: ^ (UIAlertAction * _ Nonnull action) {// click OK, you can set nil NSLog (@ "you clicked OK .. ") ;}]]; [self presentViewController: AV animated: YES completion: nil];} else {// JSON parses Apple's native most efficient NSDictionary * result = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableLeaves error: nil]; if ([result objectForKey: @ "message"] isEqualToString: @ "success"]) {// get data-> the main thread updates the UI dispatch_async (dispatch_get_main_queue (), ^ {NSDictionary * data = [result objectForKey: @ "data"]; NSString * city = [data objectForKey: @ "city"]; NSString * province = [data objectForKey: @ "province"]; NSString * belong = [NSString stringWithFormat: @ "% @ · % @", province, city]; [_ resultLB setText: belong] ;}) ;}else {NSLog (@ "No information found .... ");} NSLog (@" request successful... % @ ", result) ;}}]; // start the task [sessionTask resume];}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.