Network requests in iPhone

Source: Internet
Author: User

DetailsIPhoneMediumNetworkRequest is the content to be introduced in this article.NetworkProgramming-related content, describes in detail how to obtain or sendNetworkRequest. Let's just look at the details first.

1. Simple get requests

NetworkProgramming is something we often encounter. In the IPhone, the SDK provides good interfaces, including NSURL, NSMutableURLRequest, and NSURLConnection. Generally, we recommend that you use the Asynchronous Method of receiving data to request network connections. This type of network connection is divided into two steps. The first step is to create an NSURLConnection object and then directly call its start method to connect to the network. The second step is to use the delegate method to receive data. Here is a common method:

NetworkRequest section:

 
 
  1. NSString * urlString = [NSString stringWithFormat: @ "http://www.voland.com.cn: 8080/weather/weatherServlet? City = % @ ", kcityID];
  2. NSURL * url = [NSURL URLWithString: urlString];
  3. NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url];
  4. NSURLConnection * aUrlConnection = [[NSURLConnection alloc] initWithRequest: request delegate: self startImmediately: true];
  5. Self. urlConnection = aUrlConnection; // The urlConnection variable defined in the header file
  6. [Self. urlConnection start]; // starts to connect to the network.
  7. [AUrlConnection release];
  8. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: YES];

The received data is mainly processed here.

 
 
  1. -(Void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response {
  2. NSLog (@ "response received: % @", response );
  3. }
  4. -(Void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {
  5. NSLog (@ "received data :");
  6. }
  7. -(Void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error {
  8. NSLog (@ "data receiving error: % @", error );
  9. }
  10. -(Void) connectionDidFinishLoading :( NSURLConnection *) connection {
  11. NSLog (@ "connection completed: % @", connection );
  12. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: NO];
  13. }

Ii. Post requests

For post requests, we mainly set the NSMutableURLRequest object. In get requests, we use the default object, and the actual content of these requests can be set. After setting, the other methods are the same as those in get mode:

 
 
  1. NSString *content=[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];  
  2. [request setHTTPBody: content];    
  3. [request setHTTPMethod: @"POST"];    
  4. [request setValue:@"Close" forHTTPHeaderField:@"Connection"];    
  5. [request setValue:@"www.voland.com.cn" forHTTPHeaderField:@"Host"];    
  6. [request setValue:[NSString stirngWithFormat@"%d",[content length]] forHTTPHeaderField:@"Content-Length"]; 

Summary: DetailsIPhoneMediumNetworkThe request content has been introduced. I hope this article will help you!

Related Article

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.