随笔 - 21  文章 - 2  评论 - 36  阅读 - 93260

ios中http 和https 协议的访问

最近做个项目,开始采用的是HTTP协议实现客户端和服务器端的交互,后来需要改成HTTPS协议。在修改的过程中发现了一些问题,解决方案如下:

 

HTTP:

    NSString *urlString =[NSString stringWithFormat:@"https://127.0.0.1/default.aspx?USER=%@",@"111"];

    

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

    [request setURL:[NSURL URLWithString:urlString]];

    [request setHTTPMethod:@"GET"];

    

    NSHTTPURLResponse* urlResponse = nil;

    NSError *error = [[NSError alloc] init];

    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];

    NSMutableString *result = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    

    NSLog(@"The result string is :%@",result);   

 

 HTTPS

事件触发 

    NSString *urlString =[NSString stringWithFormat:@"https://127.0.0.1/default.aspx?USER=%@",@"111"];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]        cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5];

    //设置请求方式为get

    [request setHTTPMethod:@"GET"];

    //添加用户会话id

    [request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];

    //连接发送请求

    finished = false;

    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

//堵塞线程,等待结束

while(!finished) {

[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

}

 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response 

{}

 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 

{

    //[_waitingDialog dismissWithClickedButtonIndex:0 animated:NO];

    [connection release];

}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

}

- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection{

    return NO;

}

//下面两段是重点,要服务器端单项HTTPS 验证,iOS 客户端忽略证书验证。

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {

    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {    

    NSLog(@"didReceiveAuthenticationChallenge %@ %zd", [[challenge protectionSpace] authenticationMethod], (ssize_t) [challenge previousFailureCount]);

    

    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){

        [[challenge sender]  useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

        [[challenge sender]  continueWithoutCredentialForAuthenticationChallenge: challenge];

    }

    NSLog(@"get the whole response");

    //[receivedData setLength:0];

}

//处理数据 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

 

大家也可以参考下面一篇文章

关于在UIwebView中访问HTTPS站点的几种方法

 

posted on   心不蒙尘  阅读(39968)  评论(5编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
< 2012年3月 >
26 27 28 29 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
1 2 3 4 5 6 7

点击右上角即可分享
微信分享提示