HTTP synchronous requests and asynchronous requests for IOS programs

Source: Internet
Author: User

IOS ProgramOfHTTPSynchronizationRequestAnd asyncRequestThe solution to the problem is described in this article. There is not much content, mainly through code to achieve synchronous and asynchronousRequest, Let's take a look at the details.

Asynchronous request,

 
 
  1. NSMutableData * buf = [[NSMutableData alloc] initWithLength: 0];
  2. NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest: req delegate: self];
  3. // Triggered when the response is received
  4. -(Void) connection :( NSURLConnection *) aConnection didReceiveResponse :( NSURLResponse *) aResponse;
  5. // You can determine the returned result or process the information in the returned http header.
  6. // Call each time data is received
  7. -(Void) connection :( NSURLConnection *) aConn didReceiveData :( NSData *) data;
  8. // Therefore, in general, yes
  9. -(Void) connection :( NSURLConnection *) aConn didReceiveData :( NSData *) data
  10. {
  11. [Buf appendData: data];
  12. }
  13. // Of course, buffer is also stated in the previous initWithRequest.
  14. // Triggered when a network error occurs
  15. -(Void) connection :( NSURLConnection *) aConn didFailWithError :( NSError *) error;
  16. // Triggered when all data is received
  17. -(Void) connectionDidFinishLoading :( NSURLConnection *) aConn;

Synchronous request

 
 
  1. // For initialization, please request NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
  2. // Set the URL
  3. [Request setURL: [NSURL URLWithString: urlStr];
  4. // Set the HTTP Method
  5. [Request setHTTPMethod: @ "GET"];
  6. // Sending and receiving synchronization request. Here, the returnData is the returned data records.
  7. NSData * returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
  8. // Configure the object
  9. [Request release];

Summary:IOS ProgramOfHTTPSynchronizationRequestAnd asyncRequestI hope this article will help you solve the problem!

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.