方法一
实战:
文本复制:
//如果请求地址中出现中文需要进行url编码
NSString*path =@"http://apis.juhe.cn/cook/query?key=80996127f667eac43832103850b3b13a&menu=鱼";
path = [pathstringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSetURLQueryAllowedCharacterSet]];
NSURL*url = [NSURLURLWithString:path];
NSMutableURLRequest*request = [NSMutableURLRequestrequestWithURL:url];
方法二:
文本复制:
//第二种方法:需要第三方。(AFHTTPSessionManager)来自AFNetworking(需要的自己去下载哦)。。==========================================(文中有白色字体部分被隐藏,需要全部选中状态才能看全)
NSString*path =@"http://apis.juhe.cn/cook/query";
NSDictionary*paramsDic =@{@"key":@"80996127f667eac43832103850b3b13a",@"menu":@"鱼"};
AFHTTPSessionManager*manager = [[AFHTTPSessionManageralloc]init];
[managersetResponseSerializer:[AFHTTPResponseSerializerserializer]];
[managerGET:pathparameters:paramsDicprogress:nilsuccess:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject) {
NSDictionary*dic = [NSJSONSerializationJSONObjectWithData:responseObjectoptions:0error:nil];
NSLog(@"%@",[NSThreadcurrentThread]);
NSLog(@"%@",dic);
}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {
}];
[managerPOST:pathparameters:paramsDicprogress:nilsuccess:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject) {
NSDictionary*dic = [NSJSONSerializationJSONObjectWithData:responseObjectoptions:0error:nil];
}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {
}];