背景
1.客户需求
url:http://x.x.x.x:8080/uflow/exposeIface.do?method=isAllowLogin
{"code":0,"msg":"成功","data":{"isAllowLogin":true,"hintMessage":"请从任我行统一登陆"}}
注:isAllowLogin值为true时,允许app登陆;
2.客户是内网,我们无法访问到,于是想到了本地服务器,详细搭建见
手机app开发(iOS+Android) 本地服务器的搭建
本地服务器目录结构,如图所示:
settings.json
[
{
"include" : "asset/assetList.json"
}
]
assetList.json
[
{
"description":"模拟一个带参数的post请求",
"request":{
"uri":"/uflow/exposeIface",
"method":"post",
"forms":{
"method" : "isAllowLogin"
}
},
"response" :
{
"file" : "./asset/assetList_response.json"
}
}
]
assetList_response.json
{
"code":0,
"msg":"成功",
"data":
{
"isAllowLogin":true,
"hintMessage":"请从任我行统一登陆"
}
}
配置完以上内容后在终端里输入以下方法
cd /Users/macOne/Desktop/moco-server
//注意 -g 不是 -c
java -jar moco-runner-0.10.2-standalone.jar start -p 12306 -g settings.json
得到以下内容说明你的配置文件没有错误。Server is started!
INFO Server is started at 12306
INFO Shutdown port is 51783
上 postman
iOS 模拟器
访问: NSString *url=@"http://localhost:12306/uflow/exposeIface";
- (void)requestAssetList
{
// __weak typeof(self) weakSelf = self;
NSString *url=@"http://localhost:12306/uflow/exposeIface";
NSMutableDictionary *parameter = [NSMutableDictionary dictionary];
parameter[@"method"] = @"isAllowLogin";
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:url parameters:parameter progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"请求成功:%@",responseObject);
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
if (dict.count)
{
NSLog(@"isAllowLogin = %@",dict);
NSLog(@"hintMessage = %@",dict[@"data"][@"hintMessage"]);
NSLog(@"isAllowLogin = %@",dict[@"data"][@"isAllowLogin"]);
}
else
{
return ;
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
{
NSLog(@"获取信息失败::%@",error);
}];
}
iOS 真机
访问: NSString *url=@"http://192.168.2.114:12306/uflow/exposeIface";
- (void)requestAssetList
{
// __weak typeof(self) weakSelf = self;
NSString *url=@"http://192.168.2.114:12306/uflow/exposeIface";
NSMutableDictionary *parameter = [NSMutableDictionary dictionary];
parameter[@"method"] = @"isAllowLogin";
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:url parameters:parameter progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"请求成功:%@",responseObject);
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
if (dict.count)
{
NSLog(@"isAllowLogin = %@",dict);
NSLog(@"hintMessage = %@",dict[@"data"][@"hintMessage"]);
NSLog(@"isAllowLogin = %@",dict[@"data"][@"isAllowLogin"]);
}
else
{
return ;
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
{
NSLog(@"获取信息失败::%@",error);
}];
}
注:其中192.168.2.114为你本机 IP 地址
具体查看方法
mac电脑查看ip、MAC地址和dns配置的方法
Mac查看本机IP的终端命令
iOS开发真机调试访问本地服务器的url问题
让电脑和手机连接同一个wifi,这样手机跟电脑就连接在同一个局域网上了。然后查看本机的ip地址,例如我电脑本机的ip地址是:192.168.2.114,然后利用http://192.168.2.114:12306来访问本地的服务器了。不单只是手机可以访问,只要是连接在这个局域网上面的所有设备均能够访问到。
iOS 真机+青花瓷+Moco(本地服务器)
下载见: Charles 4.2.5 破解
配置见: Charles 安装图解(Mac 抓包工具)
参考见: 菜鸟搭建 Mock 服务器实践:Anyproxy+Moco
学习资料
Moco接口框架学习(http协议)