【iOS开源代码】(1):CCLHTTPServer
来源:互联网 发布:谷歌seo怎么做 编辑:程序博客网 时间:2024/05/29 16:30
我从没有见过比 CCLHTTPServer 更简单的 iOS HTTP 服务器了。你甚至不用创建任何子类即可使用它。对于大部分任务,这个小巧但强悍的服务器能够满足你的需要。
它简单到令人发指,因此我认为光看它的主页介绍就已经足够了:
https://github.com/cocodelabs/CCLHTTPServer
CCLHTTPServer 是一个 iOS/OSX 下的简单HTTP 服务器框架。
一、用法
以下代码创建了一个简单的 HTTP 服务器,监听指定端口,并用指定的 CCLHTTPServerResponse类(子类)进行响应。
Objective-C:
CCLHTTPServer*server = [[CCLHTTPServer alloc] initWithInterface:nil port:8080handler:^id<CCLHTTPServerResponse>(id<CCLHTTPServerRequest>request) {
NSDictionary *headers = @{
@"Content-Type":@"text/plain; charset=utf8",
};
NSData *body = [@"Hello World"dataUsingEncoding:NSUTF8StringEncoding];
return [[CCLHTTPServerResponse alloc]initWithStatusCode:200 headers:headers body:body];
}];
Switf:
varserver:CCLHTTPServer=CCLHTTPServer(interface: nil, port: 7433, handler:
{request in
let headers =["Content-Type": "text/plain; charset=utf8"]
let body = "Hello World".dataUsingEncoding(NSUTF8StringEncoding);
returnCCLHTTPServerResponse(statusCode: 200, headers: headers, body: body)
})
CCLHTTPServerResponse 必须对传递给它的request 参数进行处理。request 参数是一个实现了CCLHTTPServerRequest 协议的对象,该协议封装了各种有用的方法,你可以利用这些方法创建一个各种 response(该协议下面会详细介绍)。
CCLHTTPServer 的目的是暴露一个用于创建HTTP 服务器的标准 O-C 接口,见CLLHTTPServer/Interface 目录。也就是说,别的 HTTP 服务器也能使用这些接口,开发者不用管某个服务器的具体实现即可使用该接口编写自己的框架。
二、请求 Request
request 是一个实现了 CCLHTTPServerRequest 协议的对象,该协议暴露了 HTTP 请求的基本详细例如请求方法、路径、HTTP 版本,headers 以及正文。
@protocol CCLHTTPServerRequest<NSObject>
- (NSString *)method;
- (NSString *)path;
- (NSString *)HTTPVersion;
- (NSDictionary *)headers;
- (NSData *)body;
@end
三、响应 Responses
你可以新建一个 response 类,也可以使用的任何已经实现了 CCLHTTPServerResponse协议的已有类。当然,绝大多数情况下,使用 CCLHTTPServerResponse 足矣。
1、返回 NSData 数据
[CCLHTTPServerResponseresponseWithStatusCode:204 headers:nil body:nil];
2、返回文本数据
[CCLHTTPServerResponseresponseWithStatusCode:200
headers:nil
content:@"Hello World"
contentType:@"plain/text"];
3、返回 URL 编码的名值对
这种形式如同: name=Kyle&sex=female
[CCLHTTPServerResponseformURLEncodedResponseWithStatusCode:200
headers:nil
parameters:@{@"name": @"Kyle"}];
4、返回 JSON 数据
[CCLHTTPServerResponseJSONResponseWithStatusCode:200
headers:nil
parameters:@{@"name": @"Kyle"}];
四、安装
pod 'CCLHTTPServer'
- 【iOS开源代码】(1):CCLHTTPServer
- 【iOS开源代码】(1):CCLHTTPServer
- 【iOS开源代码】(1):CCLHTTPServer
- CCLHTTPServer
- iOS开源代码
- iOS 开源代码集合
- iOS 开源代码集合
- ios 开源代码
- IOS开源代码网址
- ios开源代码集合
- iphone ios 开源代码Tree20
- iphone ios 开源代码Tree20
- 【iOS开源代码】(2):CKEditor
- 【iOS开源代码】(3):DateTools
- 【iOS开源代码】(4):FPPopover
- 【iOS开源代码】(5):MKNetworkKit
- iOS开源代码—刮刮卡
- iOS源代码
- Java程序将输出到控制台的语句存入文件和windows DOS将文字输入到文件中
- 小黑小波比.sql语句查询0:全部;1:类型A;2:类型B
- 【更新】修改Host文件,让你的Google跑起来(2014-9-2更新)
- php 实现分页
- 在 XenServer上调试windows程序
- 【iOS开源代码】(1):CCLHTTPServer
- android NotFoundException: String resource ID #0x1
- 1009. 说反话 (20)
- DEV EXPRESS gridlookupedit和Gridview表格上面的gridlookupedit
- 如何在 AutoLayout 中使用 UIScrollView (多个ContentView)
- P2P网贷 网络投资理财平台—网络时代的特殊产物
- 布隆过滤器 (Bloom Filter) 详解
- SAP ABAP编程 Table Control动态隐藏列
- 信息无障碍大赛获奖名单出炉