IOS开发之---网络应用

NSData

Foundation

NSURL/NSURLRequest/NSURLConeection

NSNetService/NSNetServiceBrowser

Core Foundation

CFNetwork

CFNetService

BSD Sockets


[plain]  view plain copy
  1.   
(1)获取图片

[plain]  view plain copy
  1. NSData *data = [[NSData alloc] initWithContentsOfURL:url];  
  2. NSData *data = [NSData dataWithContentsOfURL:url];  

  例子:

[plain]  view plain copy
  1.  NSAutoreleasePool   *pool = [[NSAutoreleasePool alloc] init];  
  2. //根据网络数据,获得到image资源  
  3. NSData  *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:self.picUrlString]];  
  4. UIImage *image = [[UIImage alloc] initWithData:data];  
  5. [data release];  
  6. //回到主线程,显示图片信息  
  7. [self performSelectorOnMainThread:@selector(displayImage:) withObject:image waitUntilDone:NO];  
  8. [image release];  
  9.   
  10. [pool release];  

异步

[plain]  view plain copy
  1. NSURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];  
  2. NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];  


(2)GET方法

[plain]  view plain copy
  1. NSURL *url = [NSURL URLWithString:urlString];  
  2. NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];  
  3. NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] init];  
  4. NSData *retData = [NSURLConnection sendSynchronousRequest:theRequest   
  5. returningResponse:&response error:nil];  
  6. NSString *retString = [[NSString alloc] initWithData:retData encoding:NSUTF8StringEncoding];  
(3)post方法

[plain]  view plain copy
  1. NSURL *url = [NSURL URLWithString:urlString];  
  2. NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];  
  3. NSString *postString = @"test=3";  
  4. NSData *postData = [postString dataUsingEncoding:NSUTF8StringEncoding];  
  5. NSString *msgLength = [NSString stringWithFormat:@"%d", [postData length]];  
  6. [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];  
  7. [theRequest setHTTPMethod:@"POST"];  
  8. [theRequest setHTTPBody: postData];  
  9. NSHTTPURLResponse *response =[[NSHTTPURLResponse alloc] init];  
  10. NSData *retData = [NSURLConnection sendSynchronousRquest:theRequest returningResponse:&resp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值