利用charles抓取《美食杰》的业务数据
一、工具
(1) charles (抓包工具)

charles for mac 下载
(2) 美食杰 (数据来源)

AppStore搜索 美食杰
二、教程
1.打开charles

2.查看本机ip

3.在苹果设备上设置代理

4.操作美食杰app查看charles抓取到数据


注意:电脑的网络和移动设备的网路必须在同一网段里,设置完代理,charles会弹出是否允许访问的对话框,点击allow就可以了。
三、效果
1.写了个简单的http请求
封装的请求方法:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; if ([aMethod isEqualToString:GET_METHOD]) { NSMutableString *urlString = [[NSMutableString alloc] initWithString:aURLString]; NSArray *paramArray = params.allKeys; for (NSString *tempString in paramArray) { int index = (int)[paramArray indexOfObject:tempString]; [urlString appendFormat:@"&%@=%@",tempString,params[tempString]]; } request.URL = [NSURL URLWithString:urlString]; }else if ([aMethod isEqualToString:POST_METHOD]){ //请求参数 NSString *paramsJsonString = [self DataTOjsonString:params]; request.HTTPBody = [paramsJsonString dataUsingEncoding:NSUTF8StringEncoding]; request.URL = [NSURL URLWithString:aURLString]; } //请求方法 request.HTTPMethod = aMethod; [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; aSuccess([self dictionaryWithJsonString:responseString]); }];
发送请求:
NSArray *sortAray = @[@"step",@"default",@"time"]; NSArray*tArray=@[@"zao",@"zhong",@"xiawucha",@"wan",@"yexiao"]; for (int a = 0; a<1000; a++) { NSMutableDictionary *params = [NSMutableDictionary dictionary]; [params setValue:@"" forKey:@"lat"]; [params setValue:@"" forKey:@"lon"]; [params setValue:@"iphone" forKey:@"source"]; [params setValue:sortAray[random()%sortAray.count] forKey:@"sort"]; [params setValue:tArray[random()%tArray.count] forKey:@"t"]; [params setValue:@"" forKey:@"kw"]; [params setValue:@"desc" forKey:@"sort_sc"]; [params setValue:@"" forKey:@"gy"]; [params setValue:@"" forKey:@"step"]; [params setValue:@"" forKey:@"mt"]; [params setValue:[NSString stringWithFormat:@"%d",a] forKey:@"page"]; [_msjNetwork api_get_candanWithUrl:@"http://api.meishi.cc/v5/class_list1.php?format=json" params:params successBlock:^(NSDictionary *responeDictionary) { } failBlock:^(NSError *error) { }]; }
响应:把得到的数据扎入本地数据库
[self requestNetworkWithURLString:aUrl params:aParam requestMethod:GET_METHOD successBlock:^(NSDictionary *responeDictionary) { NSDictionary *temp1 = responeDictionary[@"obj"]; NSArray *temp2 = temp1[@"data"]; for (NSDictionary *temp3 in temp2) { NSString *msjID = temp3[@"id"]; MsjModel *temp = [MsjModel FindByID:msjID]; if (!temp) { temp = [[MsjModel alloc] init]; }else { continue; } temp.msjID = msjID; temp.gongyi = temp3[@"gongyi"]; temp.kouwei = temp3[@"kouwei"]; temp.mt = temp3[@"mt"]; temp.smalltext = temp3[@"smalltext"]; temp.titlepic = temp3[@"titlepic"]; temp.title = temp3[@"title"]; temp.md = temp3[@"md"]; [temp saveOrUpdateDB]; } asuccessBlock(responeDictionary); } failBlock:^(NSError *error) { }];
获取到的数据

HTTP抓包
打开Charles程序
查看Mac电脑的IP地址,如192.168.1.7
打开iOS设置,进入当前wifi连接,设置HTTP代理Group,将服务器填为上一步中获得的IP,即192.168.1.7,端口填8888
iOS设备打开你要抓包的app进行网络操作
Charles弹出确认框,点击Allow按钮即可
HTTPS抓包
下载Charles证书http://www.charlesproxy.com/ssl.zip,解压后导入到iOS设备中(将crt文件作为邮件附件发给自己,再在iOS设备中点击附件即可安装;也可上传至dropbox之类的网盘,通过safari下载安装)
在Charles的工具栏上点击设置按钮,选择Proxy Settings…
切换到SSL选项卡,选中Enable SSL Proxying,别急,选完先别关掉,还有下一步
这 一步跟Fiddler不同,Fiddler安装证书后就可以抓HTTPS网址的包了,Charles则麻烦一些,需要在上一步的SSL选项卡的 Locations表单填写要抓包的域名和端口,点击Add按钮,在弹出的表单中Host填写域名,比如填api.instagram.com,Port 填443
接下来就跟HTTP抓包一样了
抓取微信的乱码:
