Duplicate requests from HttpClient

I am using HttpClient 4.0.1 for android ... I am making a POST request with a set of headers, which is the current millis ... I can see that this request hits the server twice within a few milliseconds (5-10) of each other, but the header which I asked is the same for both queries. This happens very sporadically ... I do not see the real difference between the requests in wirehark ... I just have no idea how this could happen. Does anyone come across this before or have any tips on how to debug it?

here is the code i use to create the client:

public static HttpClient getAndroidHttpClient(final int timeOut) { // set up the schemas SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443)); // set up our params HttpParams params = new BasicHttpParams(); params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeOut); params.setIntParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, timeOut); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeOut); params.setLongParameter(ConnManagerPNames.TIMEOUT, timeOut); params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 1); params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(1)); params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false); HttpProtocolParams.setUserAgent(params, "android-client-v1.0"); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "utf8"); ThreadSafeClientConnManager conman = new ThreadSafeClientConnManager(params, schemeRegistry); DefaultHttpClient defaultHttpClient = new DefaultHttpClient(conman, params); return defaultHttpClient; } 
+8
android
source share
3 answers

So, what happens here is that your client sends a request, does not receive a response in a timely manner, and as a result repeats the same request again (as it should). This, in turn, leads to several POST requests sent to your server (almost sequentially) that your server cannot currently deal with accordingly.

To check / debug this, try disabling HTTP retries as follows:

 defaultHttpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler (0, false)); 

This, of course, will deal with your problem with duplicate queries, but then introduces another serious problem; namely, he will try once (and only once) and fail. From the information received from your comments, here are a few ideas you can try:

Voting with pseudo-code, because I do not have all the details about how your client is archived.

Process multiple POSTS in sequence

  • Disable automatic attempts (as described above)
  • Wrap your POST requests in a loop similar to how it is implemented
  • Then either sleep between your repeats manually, or implement your version of exponential backup .

Regardless of whether your server will need the ability to handle repeated requests in a reasonable way, this is HTTP afterall. However, you at least give him the opportunity to handle the first before he is bombarded with duplicates.

I recommend that the first step that is required when processing the request is to install some form (duplicate). Then, if / when it receives a cheat, it continues to process the first request (as usual) and silently ignores the cheats.

To simply summarize, the point of this whole scheme was to give your server the ability to set the dupe flag . After that, your server job discards (or processes) duplicate requests as needed. Does all this make sense?

+18
source

I can’t talk about the version of HttpClient that comes with Android, as it is actually a plug based on an extremely old snapshot before BETA. However, if you are using the fallback version of Apache HttpClient 4.x, it DOES NOT automatically repeat POST or PUT requests unless configured otherwise.

In your particular case, I suspect that the HTTP message will be sent by the wireless driver due to a loss of connection or a similar network problem. HTTP is not a guaranteed delivery protocol. HTTP messages can be resent by lower level transporters. Your application should be ready to work with repeating HTTP messages.

+1
source

Retry policy We can customize

WITH REQUEST VOLLEY

stringRequest.setRetryPolicy (new DefaultRetryPolicy (0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); volleySingleton.addToRequestQueue (stringRequest);

0
source

All Articles