码迷,mamicode.com
首页 > 其他好文 > 详细

2014.12.3 网络应用

时间:2014-12-06 10:07:11      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

NSData

Foundation

NSURL/NSURLRequest/NSURLConeection

NSNetService/NSNetServiceBrowser

Core Foundation

CFNetwork

CFNetService

BSD Sockets

 
  1.   

(1)获取图片

 

 

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

  例子:

 

 

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

 

 

异步

 

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

 

(2)GET方法

 

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

 

 

  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:&response error:nil];  

2014.12.3 网络应用

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/mybelief/p/4147826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!