2014.12.3 Network Application

Source: Internet
Author: User

NSData

Foundation

Nsurl/nsurlrequest/nsurlconeection

Nsnetservice/nsnetservicebrowser

Core Foundation

Cfnetwork

Cfnetservice

BSD Sockets

[Plain]View Plaincopyprint?

(1) Get pictures

[Plain]View Plaincopyprint?
    1. NSData *data = [[NSData alloc] initwithcontentsofurl:url];
    2. NSData *data = [NSData Datawithcontentsofurl:url];
    NSData *data = [[NSData alloc] initwithcontentsofurl:url];    NSData *data = [NSData Datawithcontentsofurl:url];

Example:

[Plain]View Plaincopyprint?
    1.  nsautoreleasepool   *pool = [[nsautoreleasepool alloc]  init];  
    2. //Based on network data, get to Image resource   
    3. nsdata  *data  = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:self.picUrlString]];   
    4. uiimage *image = [[uiimage alloc] initwithdata:data];   
    5. [DATA RELEASE];  
    6. //Return to main thread, display picture information   
    7. [self performselectoronmainthread: @selector (displayimage:)  withObject:image  waituntildone:no];  
    8. [image release];  
    9.   
    10. [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?
    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 method

[Plain]View Plaincopyprint?
    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 method

[Plain]View Plaincopyprint?
  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 Network Application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.