NSURLConnection同步,异步与SSL
来源:互联网 发布:怎么样收到淘宝店铺 编辑:程序博客网 时间:2024/05/16 12:52
通常在IPhone里用网络访问会用到NSURLConnection来做url链接请求,下面简单介绍一下:
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];
NSError *err=nil;
NSData *data=[NSURLConnection sendSynchronousRequest:request
returningResponse:nil
error:&err];
if(data==nil)
{
//if([err code])
NSLog(@"Code:%d,domain:%@,localizedDesc:%@",[err code],
[err domain],[err localizedDescription]);
}
else
{
}
这种情况,通过一个静态方法,请求request,这种情况下,会一直阻塞,等到返回结果,简单易用
2、异步请求
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:requestdelegate:self];
[url release];
[request release];
if(connection)
{
receivedData = [[NSMutableData data] retain];
NSLog(@"intial done!");
}
else
{
NSLog(@"sorry");
}
通过一个delegate来做数据的下载以及Request的接受等等消息,此处delegate:self,所以需要本类实现一些方法,并且定义receivedData做数据的接受
基本上要实现下面节歌方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response
{
NSLog(@"get the whole response");
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"get some data");
[receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connection release];
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}
while(!finished) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDatedistantFuture]];
}
好了,现在我们看看SSL的问题,在NSURLConnnection本来有方法可以跳过ssl检查,可惜被apple无情的私有了,所以同步的数据请求肯定不行了,看看文档,只能通过异步delegate的方式了
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return [protectionSpace.authenticationMethodisEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge.protectionSpace.authenticationMethodisEqualToString:NSURLAuthenticationMethodServerTrust])
if ([trustedHosts containsObject:challenge.protectionSpace.host])
[challenge.sender useCredential:[NSURLCredentialcredentialForTrust:challenge.protectionSpace.serverTrust]forAuthenticationChallenge:challenge];
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
第一个方法会根据你的URL来判断是否需要做认证
第二个方法是认证的过程,if ([trustedHosts containsObject:challenge.protectionSpace.host]),这行代码注释掉,就可以自动所有SSL通过,否则,你可以加一些Trust的hosts,其他的不通过就行了!!!
- NSURLConnection同步,异步与SSL
- NSURLConnection同步,异步与SSL
- NSURLConnection同步,异步与SSL
- NSURLConnection同步,异步与SSL
- NSURLConnection同步,异步与SSL
- NSURLConnection同步,异步与SSL
- NSURLConnection同步,异步与SSL
- NSURLConnection同步,异步与SSL
- NSURLConnection同步与异步请求
- NSURLConnection同步与异步请求
- NSURLConnection同步与异步请求
- NSURLConnection同步与异步请求
- http:nsurlconnection同步与异步
- NSURLConnection 同步异步HTTPRequest
- URLConnection同步,异步与SSL同步请求
- NSURLConnection同步和异步连接
- NSURLConnection 异步请求、同步请求
- NSURLConnection同步下载和异步下载
- 嵌入式 设置开发板相关目录为内存运行的方法
- UFLDL Tutorial_Preprocessing: PCA and Whitening
- sysenter/sysexit 原理
- 队列链式存储
- .NET 构造函数this()的意思
- NSURLConnection同步,异步与SSL
- 处理僵尸进程导致的后遗症
- Android “It would be better to be explicit if an 8k-char buffer is required”
- iOS编程技巧总结之语音篇-- OpenEars框架及其使用1
- delphi xe5 中TMemo控件的应用——for android
- 第十一章作业
- NorFlash和NandFlash的理解
- Extjs4中的分页使用(前后台结合)
- Android电池驱动