ios中http 和https 协议的访问
来源:互联网 发布:淘宝买到质量问题 编辑:程序博客网 时间:2024/06/07 03:32
最近做个项目,开始采用的是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
{
}
- ios中http 和https 协议的访问
- ios中http 和https 协议的访问
- ios中http 和https 协议的访问代码
- ios中http 和https 协议的访问
- ios中http 和https 协议的访问
- ios中http 和https 协议的访问
- ios中http 和https 协议的访问
- IOS-访问HTTP和HTTPS
- Http协议和HttpS协议的简介
- Http协议和Https协议的区别
- Http协议和Https协议的区别
- https协议和Http协议的区别
- HTTP 和 HTTPS 协议
- HTTP 和 HTTPS 协议
- HTTP 和 HTTPS 协议
- HTTP和HTTPS协议
- http和https协议
- HTTP协议和HTTPS协议
- [测试十年]搜狗测试第一年:主动反馈篇
- [搜狗测试]自动化测试后台揭秘
- ./configure,make,make install的作用
- automake连载--Linux下使用automake入门
- iOS开发之指定UIView的某几个角为圆角
- ios中http 和https 协议的访问
- ZOJ 1649 解题报告
- 君生我未生,我生君已老
- Cryptography I, Week 1: Stream Ciphers
- ORACLE 动态注册和静态注册的区别
- Oracle动态注册service name
- C# WinForm中使用WPF的控件
- C# 如何引用外部(未嵌入到项目project)的图片或资源
- ios view的frame和bounds之区别(位置和大小)