Data Request, ios Data Request

Source: Internet
Author: User

Data Request, ios Data Request

1. Get request

// Get put the transmitted data in the link address-(void) getRequest {NSString * interfaceString = @ "http://apis.baidu.com/showapi_open_bus/mobile/find"; NSString * requestContentString = @ "num = 15761672938 "; NSString * urlString = [NSString stringWithFormat: @ "% @? % @ ", InterfaceString, requestContentString]; NSURL * url = [NSURL URLWithString: [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; // for a variable request, you can add a request method and request headers or more // the time required for a timeoutInterval request exceeds the time required to stop sending this request. // The cachePolicy cached content method NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10]; NSString * apiKey = @ "e7f5ac9e7c42a6c8cb125ee1d7e8779e "; // send the apiKey to the specified request location on the server // The Key Required for forHTTPHeaderField is the key specified by the server [request addValue: apiKey forHTTPHeaderField: @ "apiKey"]; // specify the http request method. HTTPMethod = @ "GET"; // generally, the values are uppercase [NSURLConnection sendAsynchronousRequest: request queue: [[NSOperationQueue alloc] init] completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError) {NSLog (@ "% @", response); // parse the json file // convert data to the json file NSDictionary * info = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingAllowFragments error: nil]; NSLog (@ "% @", info); NSLog (@ "info = % @, % @", info [@ "showapi_res_body"] [@ "name"], info [@ "showapi_res_body"] [@ "prov"], info [@ "showapi_res_body"] [@ "city"]) ;}];}

2. Post request

// Post-(void) postRequest {NSURL * url = [NSURL URLWithString: @ "http://www.weihuok.com/customer2/GetService"]; // request parameter // PlatformType device type 3 indicates the ios device NSDictionary * dic =@{@ "PlatformType": @ "3"}; NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10]; // set the request Method request. HTTPMethod = @ "POST"; // set the request parameter // HTTPBody requires data // dataUsingEncoding to convert the string to data request. HTTPBody = [[NSString stringWithFormat: @ "% @", dic] dataUsingEncoding: Unknown]; [NSURLConnection sendAsynchronousRequest: request queue: [[NSOperationQueue alloc] init] completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError) {NSDictionary * info = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingAllowFragments error: nil]; NSLog (@ "=%@", info) ;}];}

3. HTTP Get request

Get url string address parameters boby body content

AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager];

[Manager GET: @ "" parameters: nil success: ^ (AFHTTPRequestOperation * operation, id responseObject ){

// ResponseObject request data content

} Failure: ^ (AFHTTPRequestOperation * operation, NSError * error ){

}];

4. HTTP Post request

// Post parameters

[Manager POST: @ "" parameters: @ {} success: ^ (AFHTTPRequestOperation * operation, id responseObject ){

} Failure: ^ (AFHTTPRequestOperation * operation, NSError * error ){

}];

  

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.