NSURLConnection同步,异步与SSL
2012-08-15 09:16
148 查看
原文地址:http://qzc770707.blog.163.com/blog/static/3408275320105249566560/
通常在IPhone里用网络访问会用到NSURLConnection来做url链接请求,下面简单介绍一下:
在头文件中加入该语句:
@interface CoursePJViewController :BaseViewController<UITextViewDelegate,NetAccessDelegate,NSURLConnectionDataDelegate>
1、同步请求
NSURL *url=[[NSURL alloc]initWithString:urlString];
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、异步请求
NSURL *url=[[NSURL alloc]initWithString:urlString];
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]);
}
基本上这样就搞定了!!!
但是异步模式下带来了一个新的问题,很多情况下,网络请求不在主线程,或者界面等待网络结果,不在主线程的时候,调用线程如果生命周期over,下面这些可能都没有调用到,导致得不到想要得效果,所以需要在NSURLConnection请求后面加点东西来阻塞
while(!finished) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDatedistantFuture]];
}
在头文件中加入该语句:
@interface CoursePJViewController :BaseViewController<UITextViewDelegate,NetAccessDelegate,NSURLConnectionDataDelegate>
通常在IPhone里用网络访问会用到NSURLConnection来做url链接请求,下面简单介绍一下:
在头文件中加入该语句:
@interface CoursePJViewController :BaseViewController<UITextViewDelegate,NetAccessDelegate,NSURLConnectionDataDelegate>
1、同步请求
NSURL *url=[[NSURL alloc]initWithString:urlString];
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、异步请求
NSURL *url=[[NSURL alloc]initWithString:urlString];
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]);
}
基本上这样就搞定了!!!
但是异步模式下带来了一个新的问题,很多情况下,网络请求不在主线程,或者界面等待网络结果,不在主线程的时候,调用线程如果生命周期over,下面这些可能都没有调用到,导致得不到想要得效果,所以需要在NSURLConnection请求后面加点东西来阻塞
while(!finished) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDatedistantFuture]];
}
在头文件中加入该语句:
@interface CoursePJViewController :BaseViewController<UITextViewDelegate,NetAccessDelegate,NSURLConnectionDataDelegate>
相关文章推荐
- NSURLConnection同步,异步与SSL
- NSURLConnection同步,异步与SSL
- NSURLConnection同步,异步与SSL
- NSURLConnection同步,异步与SSL
- NSURLConnection同步,异步与SSL
- NSURLConnection同步,异步与SSL
- NSURLConnection同步,异步与SSL
- NSURLConnection同步,异步与SSL
- iOS中使用NSURLConnection处理HTTP同步与异步请求
- MySQL主从复制原理、主从复制(异步)、半同步复制、基于SSL复制
- NSURLConnection的同步与异步
- NSURLConnection和NSMutableURLRequest 实现同步、异步请求 和 NSInputStream和NSMutableURLRequest-实现保存文件到服务器
- NSURLConnection 同步异步HTTPRequest
- MySQL主从复制原理、主从复制(异步)、半同步复制、基于SSL复制 推荐
- NSURLConnection同步异步方式下获取status code
- Swift正在使用NSURLConnection异步下载同步(实例解析)
- 通过 NSURLConnection实现 同步/ 异步 请求 (WXHL)
- NSURLConnection同步下载和异步下载
- [IOS_HTTP]NSURLConnection同步与异步请求
- iOS使用NSURLConnection发送同步和异步HTTP Request