NSURLConnection同步,异步与SSL
来源:互联网 发布:周杰伦secret知乎 编辑:程序博客网 时间:2024/06/09 19:08
原文地址:http://qzc770707.blog.163.com/blog/static/3408275320105249566560/
通常在IPhone里用网络访问会用到NSURLConnection来做url链接请求,下面简单介绍一下:
在头文件中加入该语句:
@interface CoursePJViewController :BaseViewController<UITextViewDelegate,NetAccessDelegate,NSURLConnectionDataDelegate>
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]];
}
在头文件中加入该语句:
@interface CoursePJViewController :BaseViewController<UITextViewDelegate,NetAccessDelegate,NSURLConnectionDataDelegate>
- 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同步下载和异步下载
- YUV采样格式
- Windows驱动开发:用C还是C++
- Android系统默认值的设置
- -stdcall详解
- ComposeShader混合渲染
- NSURLConnection同步,异步与SSL
- C++11各编译器支持情况对比
- oracle表的类型——堆组织表(默认)
- 【基础排序】快速排序
- 海量数据面试题集锦
- 如何不重新编译替换默认壁纸
- sh_page_fault函数的处理http://bbs.chinaunix.net/thread-2079251-1-1.html
- linux 下文件重命名/移动/复制命令 linux新建目录命令
- hadoop的datanode无法启动