HTTP Post requests using HttpClient take 2 seconds, why?

后端 未结 2 1923
孤街浪徒
孤街浪徒 2020-12-04 10:15
Update: Found the answer myself, see below :-)

Hi,

I\'am currently coding an android app that submits stuff in the background usin

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 10:43

    Allright, I solved this myself with some more investigation. All I had to do was to add a parameter that sets the HTTP Version to 1.1, as follows:

    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    HttpClient httpclient = new DefaultHttpClient(params);
    

    I found this thanks to the very nice HttpHelper Class from and-bookworm and some trial-and-error.

    If I remember correctly, HTTP 1.0 opens a new TCP connection for every request. Does that explain the large delay?

    A HTTP POST request now takes between 50 and 150 ms over WLAN and something between 300 and 500 ms over 3G.

提交回复
热议问题