Today, an asihttprequest bug is found in the project. This bug may cause your HTTP request latency to be at least after the time specified in timeout. What's even more terrible is that it may waste more time for programmers to find out this bug. I have struggled for at least three hours to solve this problem!
The project has been using asihttprequest, but once I found that HTTP requests always fail. I subconsciously thought this was a network latency problem. I extended the asihttprequest timeout time to 30 seconds, so the program no longer reports an error.
But when I used the same code today, I found that I was all wrong. This is not a timeout issue, but a bug in asihttprequest.
This time, I requested a JSP page of the local Tomcat server. The page code is very simple and the response time for requesting this page in Firefox will not exceed 0.01 seconds, however, it took more than 20 seconds to return in the iPhone simulator. This cannot be the cause of network latency.
When I change the timeout time to 10 seconds and wait for the timeout time to end, asihttprequest even directly reports the kcfstreameventerroccurred error.
I opened all the debugging logs of asihttprequest and found that asihttprequest seemed to be trying to connect to the server with the last connection:
Request #3 will use connection #2
Request attempted to use connection #2, but it has been closed -will retry with a new connection
Request #3 will use connection #3
It seems that the last HTTP connection is reused during the connection. This is definitely not acceptable, because the server has closed the connection after the last HTTP request.
So we can only disable connection reuse of asihttprequest:
[Requestsetshouldattemptpersistentconnection: No];
In this way, the previous problems will not occur. In fact, persistentconnection should not be enabled in different HTTP sessions.