Other features of ASIHTTPRequest for iOS development

Source: Internet
Author: User

Set user agent

Set the user agent as follows:

 
 
  1. [ASIHTTPRequest setDefaultUserAgentString:@"MyApp 1.0"]; 

If no user agent is set, ASIHTTPRequest will create one for you. For example, Mac OS program ):

My Application 1.0 (Macintosh; Mac OS X 10.5.7; en_GB)

You can also set the user agent for each request:

 
 
  1. [request setUserAgent:@"MyApp 1.0"]; 
When the program runs in the background, continue to execute requestiOS)
 
 
  1. // IOS 4 or above
  2. [Request setShouldContinueWhenAppEntersBackground: YES];
Monitor network activities
 
 
  1. // Record the average traffic bytes in the past 5 seconds/second
  2. NSLog (@ "% llu", [ASIHTTPRequest averageBandwidthUsedPerSecond]);
  3. If ([ASIHTTPRequest isNetworkInUse]) {
  4. // Requests in the ASIHTTPRequest process is using the network
  5. }
Disable automatic update of network connection identifier status iOS)

By default, ASIHTTPRequest automatically displays the network connection identifier in the iOS status bar when the request uses a network connection ). If you want to control the identifier, you can disable this feature:

 
 
  1. [ASIHTTPRequest setShouldUpdateNetworkActivityIndicator:NO]; 
Automatic retry upon timeout

Set the maximum number of automatic retries for timeout to 2:

 
 
  1. [request setNumberOfTimesToRetryOnTimeout:2]; 
Set persistent connection

By default, ASIHTTPRequest will try to maintain a connection to a server so that requests connected to the server can be reused. This feature can significantly increase the speed, especially when you want to request a large amount of small data ). When you connect to the HTTP 1.1 server or the server sends the keep-alive header, the persistent connection is automatically used. When the server explicitly sends the "Connection: close" header, persistent connections will not be used. In addition, by default, ASIHTTPRequest does not use persistent connections for requests that contain request bodies such as POST/PUT) from version 1.8.1 ). By setting the request, you can force such requests to use persistent connections:

 
 
  1. [request setRequestMethod:@"PUT"]; 
  2. [request setShouldAttemptPersistentConnection:YES]; 

Many servers do not specify the persistent connection duration in the Response Header. They may close the connection when any request is complete. If a server does not specify a persistent connection time, ASIHTTPRequest will keep the connection for 60 seconds after a request is complete. When setting your server, 60 may be very long or very short.

If the timeout time is too long, the server may have closed the connection when a request uses this connection. When ASIHTTPRequest encounters a connection closed error, it will retry the request on a new connection.

If the timeout time is too short, but the server wants to keep the connection for a longer time, But ASIHTTPRequest has enabled unnecessary new connections, this will reduce the efficiency.

 
 
  1. // Set the timeout value of the persistent connection to 120 seconds.
  2. [Request setPersistentConnectionTimeoutSeconds: 120];
  3. // Permanently disable persistent connections
  4. [Request setShouldAttemptPersistentConnection: NO];
Force HTTP 1.0
 
 
  1. [request setUseHTTPVersionOne:YES]; 
Disable security certificate verification

If you have a self-signed certificate, you may want to disable certificate verification for testing. Here, I suggest you purchase a certificate from a trusted CA, and dream about the production phase of the app: the app has a testing phase, and so on ?) Enable certificate verification.

 
 
  1. [request setValidatesSecureCertificate:NO]; 

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.