Set user agent
Set the user agent as follows:
- [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:
- [request setUserAgent:@"MyApp 1.0"];
When the program runs in the background, continue to execute requestiOS)
- // IOS 4 or above
- [Request setShouldContinueWhenAppEntersBackground: YES];
Monitor network activities
- // Record the average traffic bytes in the past 5 seconds/second
- NSLog (@ "% llu", [ASIHTTPRequest averageBandwidthUsedPerSecond]);
- If ([ASIHTTPRequest isNetworkInUse]) {
- // Requests in the ASIHTTPRequest process is using the network
- }
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:
- [ASIHTTPRequest setShouldUpdateNetworkActivityIndicator:NO];
Automatic retry upon timeout
Set the maximum number of automatic retries for timeout to 2:
- [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:
- [request setRequestMethod:@"PUT"];
- [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.
- // Set the timeout value of the persistent connection to 120 seconds.
- [Request setPersistentConnectionTimeoutSeconds: 120];
- // Permanently disable persistent connections
- [Request setShouldAttemptPersistentConnection: NO];
Force HTTP 1.0
- [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.
- [request setValidatesSecureCertificate:NO];