用 CocoaHttpServer 实现 https 服务
来源:互联网 发布:雷欧mac全灭图解 编辑:程序博客网 时间:2024/06/05 00:24
继承 HTTPConnection 类:
@interface MyHTTPConnection : HTTPConnection
在 HTTPConnection
子类中,实现 - (BOOL)isSecureServer 方法并返回 YES,表示支持 https。
制作一个 SSL 自签名证书,安装到钥匙串,然后导出为 .p12 文件,记住导出密码。
制作证书时需要注意 /CN 参数必须指定主机名,比如 /CN 参数指定为 127.0.0.1,则访问时只能用 https://127.0.0.1,而不能用 https://localhost 替代。
实现 sslIdentityAndCertificates 方法:
- (NSArray *)sslIdentityAndCertificates{ SecIdentityRef identityRef = NULL; SecCertificateRef certificateRef = NULL; SecTrustRef trustRef = NULL; NSString *thePath = [[NSBundle mainBundle] pathForResource:@"127.0.0.1" ofType:@"p12"]; NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath]; CFDataRef inPKCS12Data = (__bridge CFDataRef)PKCS12Data; CFStringRef password = CFSTR("你导出 .p12 的密码"); const void *keys[] = { kSecImportExportPassphrase }; const void *values[] = { password }; CFDictionaryRef optionsDictionary = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL); CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL); OSStatus securityError = errSecSuccess; securityError = SecPKCS12Import(inPKCS12Data, optionsDictionary, &items); if (securityError == 0) { CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex (items, 0); const void *tempIdentity = NULL; tempIdentity = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemIdentity); identityRef = (SecIdentityRef)tempIdentity; const void *tempTrust = NULL; tempTrust = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemTrust); trustRef = (SecTrustRef)tempTrust; } else { NSLog(@"Failed with error code %d",(int)securityError); return nil; } SecIdentityCopyCertificate(identityRef, &certificateRef); NSArray *result = [[NSArray alloc] initWithObjects:(__bridge id)identityRef, (__bridge id)certificateRef, nil]; return result;}
复制 HTTPConnetion.m 中的 startConnection 方法和 startReadingRequest 方法代码。
找到 startConnection 方法中如下语句:
[settings setObject:(NSString *)kCFStreamSocketSecurityLevelNegotiatedSSL forKey:(NSString *)kCFStreamSSLLevel]
替换为:
[settings setObject:[NSNumber numberWithInteger:2] forKey:GCDAsyncSocketSSLProtocolVersionMin];[settings setObject:[NSNumber numberWithInteger:2] forKey:GCDAsyncSocketSSLProtocolVersionMax];
在启动 HTTPServer (调用 startServer )之前,使用 setConnectionClass 方法将 HTTPConnecdtion 替换为 MyHTTPConnection:
[httpServer setConnectionClass:[MyHTTPConnection class]];
0 0
- 用 CocoaHttpServer 实现 https 服务
- 利用CocoaHTTPServer实现wifi局域网传输文件到iphone
- 利用CocoaHTTPServer实现wifi局域网传输文件到iphone
- IOS 用wifi做服务器的解决方案 CocoaHttpServer
- CocoaHTTPServer-master
- CocoaHttpserver初识
- 用TLS实现安全TCP传输及配置和访问https的web服务
- [绍棠] 利用CocoaHTTPServer实现WIFI局域网传输文件到iPhone, 实现文件上传
- suse下apache安装和SSL实现https服务
- https与SSL协议详解及Java实现免证书访问https服务代码
- CocoaHTTPServer-master 2
- Android访问https服务
- 开启 Tomcat https 服务
- nodejs创建https服务
- NodeJs创建https 服务
- nginx配置https服务
- wso2esb--https服务配置
- tomcat添加https服务
- XZ_iOS之实现textField限制只能输入数字,无论是第三方键盘还是自带键盘
- Android 重写系统Crash处理类,保存Crash信息到SD卡
- JS中的html属性操作
- RT-thread内核之对象管理系统
- iOS图层layer
- 用 CocoaHttpServer 实现 https 服务
- 安卓微信端webapp开发的那些坑
- SQL Server delete all tables
- cocos2dx 3 Layer
- Mac上更新Ruby
- Android中使用dimen定义尺寸
- Maven传递依赖的解决方式
- SharePoint 2013 使用Javascript 为人员控件赋值。(Set peoplepicker value use JS)
- linux 创建用户