您的位置:首页 > 理论基础 > 计算机网络

IOS开发之---网络应用

2012-08-16 17:48 323 查看
NSData

Foundation

NSURL/NSURLRequest/NSURLConeection

NSNetService/NSNetServiceBrowser

Core Foundation

CFNetwork

CFNetService

BSD Sockets

(1)获取图片
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
NSData *data = [NSData dataWithContentsOfURL:url];

  例子:

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];


异步

NSURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];


(2)GET方法

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方法

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];


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息