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 ){
}];