This discussion is archived
3 Replies Latest reply: Jun 13, 2011 7:27 PM by 799186 RSS

HTTP request is sent twice with a single HTTP request

799186 Newbie
Currently Being Moderated
Hi guys, I'm having this problem on my j2me app and the problem is the app is sending the http request twice when the server is busy with other processing. Let me explain in more detail:

My apps is use to check production planning schedule and sales ordering and the server (which receive the sales request) which located at the HQ also host the main production planning system. Under normal usage the sales ordering can be done in less than 5 secs but when the server is busy, the sales ordering can be longer. However, sometimes the sales order request can be very slow until the mobile app resend the sales order request again because there's no reply from the server. This causing the server to receive duplicate orders. So I was wondering whether I can do something to prevent this from happening such as blocking the second request.


Below is a portion of the code in use for HTTPconnection:
private String getRequestURL(String s) {
  StringBuffer request = new StringBuffer(s);
  return request.toString();
}

public String getValue(String s) throws java.io.IOException {
  String request = getRequestURL(s);
  HttpConnection c  = (HttpConnection)Connector.open(request, Connector.READ_WRITE);
  InputStream      ch_is = c.openInputStream();
  Reader is = new InputStreamReader(ch_is, "UTF-8"); //display chinese font
  StringBuffer     sb = new StringBuffer();
  int              ch;
  while ((ch = is.read()) != -1) {
    sb.append((char)ch);
  }
  is.close();
  c.close();
  return sb.toString();
}
Edited by: EJP on 8/06/2011 14:01: added {noformat}
{noformat} tags. Please use them.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  • 1. Re: HTTP request is sent twice with a single HTTP request
    EJP Guru
    Currently Being Moderated
    the mobile app resend the sales order request again because there's no reply from the server. This causing the server to receive duplicate orders
    If the mobile does this automatically, have it include a unique sequence number in the request, and have the server respond to duplicate requests with the same response it gave to the originall request. This is called 'idempotence' and it is an important property of any transaction system.

    By the way:
    private String getRequestURL(String s) {
    StringBuffer request = new StringBuffer(s);
    return request.toString();
    }
    You realize that this method does nothing? just returns the input argument? What's the point?
  • 2. Re: HTTP request is sent twice with a single HTTP request
    799186 Newbie
    Currently Being Moderated
    Thx for the reply... You're right the getRequestURL does nothing and I'll take it out. I add duplicate order control on the server to prevent the double ordering and it works. So the problem where receive duplicate order has been solved. However, when the server is busy processing other task it will put my order in queue and there won't be any reply after 75 seconds. I think the phone has timeout at 75s. The phone I tested will force an exception after 75 seconds on the below code (because there are no reply from the server):

    InputStream ch_is = c.openInputStream();

    After the exception you can continue use the apps but the reply which you suppose to receive won't be capture anymore. Will the 'idempotence' help this problem? Or there are other way to extend the timeout?
  • 3. Re: HTTP request is sent twice with a single HTTP request
    EJP Guru
    Currently Being Moderated
    Idempotence means you can submit the same request again harmlessly. So submit it again. With the same sequence number.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points