IOS network programming: HTTP

Source: Internet
Author: User
Tags define local

HTTP defines a way to transmit data between the server and the client.

A URL defines a way to uniquely identify the location of a resource on the network.

 

REQUESTS and RESPONSES:

The client first establishes a TCP connection and then sends a request. After the server receives the request, it sends a response to send data to the client. Then the client can continue to send the request or close the TCP connection.

HTTPS:
After a TCP connection is established and a request is sent, an SSL session needs to be established.

Request methods and their usage

 

Note: POST is required to send a large amount of data to the server, because GET only supports sending a small amount of data (8 KB ).

 

IOS's NSURLRequest and its subclass NSMutableURLRequest provide the method for creating an HTTP request.

NSURLResponse and its subclass NSHTTPURLResponse process the returned data.

URL:

 

Protocols include HTTP, FTP, and file.

URL encoding:

NSString *urlString = ; 
NSString *encoded =

NSURL is used to manage URLs.

 

Ios http apis:

The following classes are involved:

NSURL, NSURLRequest, NSURLConnection, and NSURLResponse.

1. NSURL

NSURL can define local files and network files

NSURL *url = [NSURL urlWithString:@"http://www.google.com"]; 
NSData *data = [NSData dataWithContentsOfURL:url];

NSURL defines many accessors:

 (url.port == nil) { 
  NSLog(

2. NSURLRequest

After NSURL is created, you can use NSURLRequest to create a request:

NSURL *url = [NSURL URLWithString:  (url ==); 

*request = [NSURLRequest requestWithURL:url];

(request ==

NSMutableURLRequest is a subclass of NSURLRequest. It provides methods to change the request attributes:

NSURL *url = [NSURL urlWithString]; 
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:

If you want to send an image or video, you need to use NSInputStream, which does not add all the data to the memory.

NSMutableURLRequest *request =*inStream =];

 

3. NSURLResponse

 

 

4. NSURLConnection

Initializes, starts, and cancels a connection.

Send a synchronization request:

- (NSArray *) doSyncRequest:(NSString *    NSURL *url =    NSURLRequest *request =    NSHTTPURLResponse   ***data =&&     (error !=     ([response isKindOfClass:[NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse * (httpResponse.statusCode !=     NSDictionary *dictionary =&    NSLog(*entries =    

 

 

Queued Asynchronous Requests:

- () doQueuedRequest:(NSString *)urlString  :()    NSURL *url =    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:     (queue ===^(NSURLResponse *** (error !=                 ([response isKindOfClass:[NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse * (httpResponse.statusCode !=                 NSDictionary *dictionary =&                NSArray *entries =                 ([

 

NSURLConnection start method and Delegate:

GET:

 NSURL  *url=*request=*connection = [[ NSURLConnection   alloc ]  initWithRequest :request - ()connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *  [activityIndicator startAnimating]; //UIActivityIndicatorView    NSLog(=- ()connection:(NSURLConnection*)connection didReceiveData:(NSData*    [responseData appendData:data]; - ()connection:(NSURLConnection*)connection didFailWithError:(NSError*- ()connectionDidFinishLoading:(NSURLConnection *

[ActivityIndicator stopAnimating];

}

POST:

    NSMutableData *data ==    NSURL *url = [NSURL URLWithString:    NSMutableURLRequest *request =    [request setHTTPMethod:    NSString *postData = [[NSString alloc] initWithString: forHTTPHeaderField:    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request =    [connection start];

Asynchronous request:
An asynchronous request requires a run loop to operate the proxy object. GCD and NSOperationQueue do not have a run loop by default. Therefore, if you want to initiate an HTTP request in the background, you must ensure that a run loop exists.

NSURLConnection connection =

The above code is run in the main thread. If you want to run in another thread, you can create a run loop in another thread and bind it to connection.

 

 

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.