Test the Android http timeout Option

Source: Internet
Author: User

Android provides two timeout settings for http parameter settings through the HttpConnectionParams class, namely setSoTimeout and setConnectionTimeout. At first glance, the official documentation provided by Android does not clearly understand the specific meanings of the two methods, so we decided to test the two parameters to see under what circumstances the two parameters will take effect.

After testing, it can be found that setConnectionTimeout sets the connection establishment timeout. This is for TCP three-way handshake. If a TCP connection cannot be established with the http server within the specified time, ConnectionTimeoutException will be thrown. SetSoTimeout sets the TCP retention time. If the corresponding packet sent from the server is not received within the specified time after the connection is established, SocketTimeoutException is thrown.

I. Test code
 
        private String urlString;        private int timeout1,timeout2;        MyAsyncTask(String url,int timeout1,int timeout2){            urlString=url;            this.timeout1=timeout1;            this.timeout2=timeout2;        }        @Override        protected String doInBackground(String... params) {            httpget(urlString,timeout1,timeout2);            return "test";        }    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        new MyAsyncTask("http://www.xiaonei.com:1234",3000,5000).execute("start");        new MyAsyncTask("http://www.baidu.com",3000,5000).execute("start");            }    private void httpget(String Url,int timeout1,int timeout2) {        Log.v("httpget", "httpget start timeout1 is "+timeout1+"timeout2 is "+timeout2);        int timeoutConnection = timeout1; // until connection is established        int timeoutSocket = timeout2; // timeout for waiting for data        HttpParams httpParameters = new BasicHttpParams();        HttpConnectionParams.setConnectionTimeout(httpParameters,                timeoutConnection);        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);            DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);        HttpGet postRequest = new HttpGet(Url);            HttpResponse httpResp = null;            try {            httpResp = httpClient.execute(postRequest);        } catch (ClientProtocolException e) {            Log.v("Main", "clinet protocol exception");            return;        }catch (SocketTimeoutException e) {            // TODO: handle exception            Log.v("Main", "socket timeout"+timeout2);            return;        }catch (ConnectTimeoutException e) {            // TODO: handle exception            Log.v("Main", "connection timeout"+timeout1);            return;        }        catch (IOException e) {            Log.v("Main", "io exception");            return;        }        }

 

Ii. setConnectionTimeout Test

A ConnnectionTimeoutException occurs when an IP address such as "10.1.1.1" or a public server port such as "www.xiaonei.com: 1234" is written into the httpget url. for an IP address that cannot be communicated, the Android system directly constructs a TCP connection data packet sent to the changed address. However, because the address does not exist, no confirmation is received, a timeout event occurs when the set timeout time is reached. It is similar for situations where no port number exists.

Iii. setSoTimeout Test

SetSoTimeout sets the timeout value for socket persistence. The test method is to set a proxy for the Android terminal. After the agent receives an http request from the Android terminal, it directly intercepts the http request and does not send it. When the timeout time is reached, socketTimeoutException is thrown. such an http packet interception method can be easily implemented through fiddler. For how to configure Androidhttp packet capture by using the fiddler proxy function, see manage? Id = 3509.

After the fiddler proxy is configured and the http packet is intercepted, run the test program to get the following output. Both http requests output socket timeout. The first request also outputs socket timeout because a connection can be established between the Android terminal and the proxy, so no connection timeout occurs, however, after the proxy transfers an http request from the Android terminal, it cannot obtain a response, and the corresponding information cannot be fed back to the client. The client has a socket timeout.

Iv. Further understanding of AsyncTask

From the above test results, we can see that although the AsyncTask class is called in the oncreate function to send two different http requests, however, the second called Asyntask can only be executed after the first called times out. This is because after Android 3.0, the implementation of AsyncTask puts all Asynctask into a thread and executes them sequentially. For more detailed analysis, see http://blog.csdn.net/singwhatiwanna/article/details/17596225.

V. References

[1] explanation of http timeout parameters: http://stackoverflow.com/a/18185568/1767800

[2] about how to get http connection Timeout: http://stackoverflow.com/questions/100841/artificially-create-a-connection-timeout-error

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.