首页 > 代码库 > 2014.12.3 网络应用
2014.12.3 网络应用
NSData
Foundation
NSURL/NSURLRequest/NSURLConeection
NSNetService/NSNetServiceBrowser
Core Foundation
CFNetwork
CFNetService
BSD Sockets
[plain] view plaincopyprint?
(1)获取图片
[plain] view plaincopyprint?
- NSData *data = [[NSData alloc] initWithContentsOfURL:url];
- NSData *data = [NSData dataWithContentsOfURL:url];
NSData *data = http://www.mamicode.com/[[NSData alloc] initWithContentsOfURL:url];>例子:
[plain] view plaincopyprint?
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- //根据网络数据,获得到image资源
- NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:self.picUrlString]];
- UIImage *image = [[UIImage alloc] initWithData:data];
- [data release];
- //回到主线程,显示图片信息
- [self performSelectorOnMainThread:@selector(displayImage:) withObject:image waitUntilDone:NO];
- [image release];
- [pool release];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //根据网络数据,获得到image资源 NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:self.picUrlString]]; UIImage *image = [[UIImage alloc] initWithData:data]; [data release]; //回到主线程,显示图片信息 [self performSelectorOnMainThread:@selector(displayImage:) withObject:image waitUntilDone:NO]; [image release]; [pool release];
异步
[plain] view plaincopyprint?
- NSURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
- NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
NSURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
(2)GET方法
[plain] view plaincopyprint?
- NSURL *url = [NSURL URLWithString:urlString];
- NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
- NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] init];
- NSData *retData = [NSURLConnection sendSynchronousRequest:theRequest
- returningResponse:&response error:nil];
- NSString *retString = [[NSString alloc] initWithData:retData encoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlString];NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] init];NSData *retData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:nil];NSString *retString = [[NSString alloc] initWithData:retData encoding:NSUTF8StringEncoding];(3)post方法
[plain] view plaincopyprint?
- NSURL *url = [NSURL URLWithString:urlString];
- NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
- NSString *postString = @"test=3";
- NSData *postData = [postString dataUsingEncoding:NSUTF8StringEncoding];
- NSString *msgLength = [NSString stringWithFormat:@"%d", [postData length]];
- [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
- [theRequest setHTTPMethod:@"POST"];
- [theRequest setHTTPBody: postData];
- NSHTTPURLResponse *response =[[NSHTTPURLResponse alloc] init];
- NSData *retData = [NSURLConnection sendSynchronousRquest:theRequest returningResponse:&response error:nil];
2014.12.3 网络应用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。