NSData
Foundation
Nsurl/nsurlrequest/nsurlconeection
Nsnetservice/nsnetservicebrowser
Core Foundation
Cfnetwork
Cfnetservice
BSD Sockets
[Plain]View Plaincopyprint?
(1) Get pictures
[Plain]View Plaincopyprint?
- NSData *data = [[NSData alloc] initwithcontentsofurl:url];
- NSData *data = [NSData Datawithcontentsofurl:url];
NSData *data = [[NSData alloc] initwithcontentsofurl:url]; NSData *data = [NSData Datawithcontentsofurl:url];
Example:
[Plain]View Plaincopyprint?
- nsautoreleasepool *pool = [[nsautoreleasepool alloc] init];
- //Based on network data, get to Image resource
- nsdata *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:self.picUrlString]];
- uiimage *image = [[uiimage alloc] initwithdata:data];
- [DATA RELEASE];  
- //Return to main thread, display picture information
- [self performselectoronmainthread: @selector (displayimage:) withObject:image waituntildone:no];
- [image release];
-   
- [pool release];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; Based on network data, get to Image resource nsdata *data = [[NSData alloc] Initwithcontentsofurl:[nsurl urlwithstring: Self.picurlstring]]; UIImage *image = [[UIImage alloc] initwithdata:data]; [Data release]; Back to the main thread, display the picture information [self performselectoronmainthread: @selector (displayimage:) withobject:image Waituntildone:no]; [Image release]; [Pool release];
Asynchronous
[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 method
[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 method
[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 Network Application