ios扩展http服务,将iphone做小型的http服务器
2016-06-24 00:00
423 查看
摘要: ios扩展http服务,将iphone做小型的http服务器.根据http://blog.csdn.net/zhibudefeng/article/details/9125783的方法整理好NewIdeasAPI_LocHttpServer.framework框架.使用方法如下
AppDelegate.h
ViewController.h
Settings.plist
我整理的一个ios扩展http服务 NewIdeasAPI_LocHttpServer.framework
上面demo下载地址:http://git.oschina.net/shareDemoCode/IosHttpServer
AppDelegate.h
// // AppDelegate.m // IosHttpServer // // Created by 罗若文 on 16/6/24. // Copyright © 2016年 罗若文. All rights reserved. // #import "AppDelegate.h" #import <NewIdeasAPI_LocHttpServer/NewIdeasAPI_LocHttpServer.h> @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //[NIHTTPServer setPort:8080 withAction:@[@"/",@"/1/1.do"]];//如果没有配置Settings.plist可以用此方法配置端口和action路径 [[NIHTTPServer sharedNIHTTPServer] start];//开启http服务 return YES; } #pragma mark - 程序意外暂行 - (void)applicationWillTerminate:(UIApplication *)application { [[NIHTTPServer sharedNIHTTPServer] stop]; } @end
ViewController.h
// // ViewController.m // IosHttpServer // // Created by 罗若文 on 16/6/24. // Copyright © 2016年 罗若文. All rights reserved. // #import "ViewController.h" #import <NewIdeasAPI_LocHttpServer/NewIdeasAPI_LocHttpServer.h> @interface ViewController () @property (strong, nonatomic) IBOutlet UIButton *localhost; @property (strong, nonatomic) IBOutlet UIButton *loaclhost_1; @property (strong, nonatomic) IBOutlet UIButton *localhost_login; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //局域网内可以用浏览器访问http://手机ip:端口/1/1.do 得到该数据 [NIHTTPServer setActionResponseHtml:@"<html><head><title>测试</title></head><body><h1>html页面</h1><p>内容</p></body></html>" action:@"/1/1.do"]; //局域网内可以用浏览器访问http://手机ip:端口/login.do 得到该数据 [NIHTTPServer setActionResponseData:@"这边可以设置/login.do路径下可以得到的字符串" action:@"/login.do"]; //这方法可以设置在请求本机http服务的时候,在Response返回数据之前执行testHttp方法 [NIHTTPServer setMethodForHTTPServer:@selector(testHttp:) methodObject:self]; } -(void)testHttp:(NSString *)action{ //这边可以拦截action,判断做对应的操作,可以控制对应的action要返回的动态数据 if([@"/" isEqualToString:action]){ //同步请求数据 NSString *dataUrl = [NSString stringWithFormat:@"https://git.oschina.net/luoruowen/LRWplist/raw/master/Mbomc.plist"]; dataUrl = [dataUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:dataUrl]; NSString *responseString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; //将数据放入到 [NIHTTPServer setActionResponseData:responseString action:action]; } } - (IBAction)localhost:(UIButton *)sender { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://127.0.0.1:8080/"]]; } - (IBAction)loaclhost_1:(UIButton *)sender { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://127.0.0.1:8080/1/1.do"]]; } - (IBAction)localhost_login:(UIButton *)sender { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://127.0.0.1:8080/login.do"]]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
Settings.plist
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>HTTPServerConfigure</key> <dict> <key>说明</key> <string>这边配置扩展http服务端口(默认8080)和action路径(action路径至少要配置一个"/")</string> <key>HTTP_SERVER_PORT</key> <real>8080</real> <key>HTTP_SERVER_ACTION</key> <array> <string>/</string> <string>/login.do</string> <string>/1/1.do</string> </array> </dict> </dict> </plist>
我整理的一个ios扩展http服务 NewIdeasAPI_LocHttpServer.framework
上面demo下载地址:http://git.oschina.net/shareDemoCode/IosHttpServer
相关文章推荐
- Visualizing MNIST 降维探索
- http-关于application/x-www-form-urlencoded等字符编码的解释说明
- android之网络操作(1)
- EntityFramework.SqlServer.dll 中发生 其他信息: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - 无法打开到 SQL Server 的连接)
- HTTP网页错误代码---网络、端口
- Python网络爬虫(1)
- Xml解析网络数据
- Http请求模拟报文返回工具HttpSimulation分享
- HttpServletRequest 和 HttpServletResponse对象
- 思科基础命令大全
- Android Https相关完全解析 当OkHttp遇到Https
- 神经网络Trick之DropConnect
- 子网划分举例
- 在MVC里使用 HttpContext.Response输出内容
- 网络编程socket(服务端简单模拟)(OC)
- 神经网络的Trick之Dropout的理解与实现
- Unable to download data from http://ruby.taobao.org/ & don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
- HTTPS的学习与使用
- iOS基础:网络相关
- 无线Mesh网络总结(新)