Set User Agent
The Code is 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 the request (IOS)
// IOS 4 or above, supports background download, but only supports 1 minute. After 1 minute, IOS will kill the program
[request setShouldContinueWhenAppEntersBackground:YES];
Monitor network activities
// Record the average traffic bytes in the past 5 seconds/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 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 reuse the connection (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 containing request bodies (such as post/Put) (starting from 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 the persistent connection [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 use it for production-related apps (mengwei: Does the app still have a testing phase ?) Enable certificate verification.
[request setValidatesSecureCertificate:NO];
Read local files as a stream and upload them to the server
Asiformdatarequests settings
[Request setfile: @ "/users/BEN/desktop/bigfile.txt" forkey: @ "file"]; // when setfile: forkey: is used, asiformdatarequest uploads files to the server using a stream.
Settings of normal asihttprequest:
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com/"];ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];[request setShouldStreamPostDataFromDisk:YES];[request appendPostData:data];[request appendPostDataFromFile:@"/Users/XXX/Desktop/bigfile.txt"];[request startSynchronous];