×

Discussion Board

Results 1 to 9 of 9
  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    Unhappy HTTP Connection sending request twice after 30 sec

    Hello everyone,
    I m writing one application in which i m posting data on server side using HTTP Connection.Some How my server taking much time to generate response,so after 45 sec my c2 phone automatically sends same request to server if connection is unable to get response within 45 time.I have checked log on server side and i found that my app is sending two request insted of one after 45 sec.i have also checked on emulator and havent facing this type of issue in emulator.

    here i have atteched my code.

    HttpConnection connection = null;

    String url="some URL";
    String param="function=OpenRecharge&LoginId=s10dq9zq&BatchId=5&SystemServiceID=4&ReferalNumber=88888888888&Amount=250000&FromANI=0782283987&Email=&Checksum=283c9abc5d12a1de939551e88514cccc";
    connection= (HttpConnection) Connector.open(url,Connector.READ_WRITE,true);//,Connector.READ_WRITE,true);
    connection.setRequestMethod(HttpConnection.POST);
    connection.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
    connection.setRequestProperty("Accept_Language","en-US");
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    OutputStream out = connection.openOutputStream();

    out.write(param.getBytes());

    out.flush();
    out.close();

    Alert error=new Alert("ResponseCode:",""+connection.getResponseCode(),null,AlertType.INFO);
    error.setTimeout(Alert.FOREVER);
    display.setCurrent(error);


    can anyone tell me why my app sending two request from only phone.??or can anyone suggest me how to increse htttpconnection timout..??

  2. #2
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: HTTP Connection sending request twice after 30 sec

    Hi nrv124u and Welcome to Nokia's Discussion Boards,

    I can see you have provided the code where you POST data to the server, but since you wait 45 seconds, this rather seems to be a control flow issue. How do you re-attempt to POST data? Have you placed this code within a thread that sleeps for 45 seconds after it POSTS the data, then wakes up and re-attempts the operation? There is an example on how to use threads here in case you haven't.

    Could it be that the server logs the first request with some delay and eventually displays both requests one after the other as if they had been received simultaneously?

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    Re: HTTP Connection sending request twice after 30 sec

    no buddy...i m not doing any code to sleep my thread..my s40 series phone automatically sends same request after 30 sec..i have also checked same app in s60 series..and its working fine..can u tell me how to avoid this situation in s40 seres phone.

  4. #4
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: HTTP Connection sending request twice after 30 sec

    So, if I understand correctly, your Java client on your C2-XX device, silently retries to Post the request to the server without your consent. I have been investigating this a bit and it seems that, this was a known issue for Java SE clients up to version 1.5, when the HTTP server delayed to reply and when the server did not send back an HTTP response or an IOException occurred. The Java SE client then attempted to reconnect and retry to POST once more, silently. Given that POST (unlike GET) is not an idempotent operation, it should not be retried. Since a lot of applications by then were built, by taking this behavior for granted, the fix that came with Java SE version 1.6, which was the method retryPost(boolean b), had a default value true. By setting the boolean value to false, in the latest Java SE versions, it is now possible to prevent this behavior. Unfortunately, retryPost does not come in Java ME, and the client cannot really control the timeout with JSR-118. So I don't see how this can be prevented in Java ME.

    Two things that might be worth of trying:
    • Try to add the following header
      Code:
      connection.setRequestProperty("Connection", "close");
      This signals that the connection is not persistent and should close after the response. The client might not re-try to silently POST in a non persistent connection.
    • Try to increase the keepAlive timeout Directive on server side as described here. By keeping the connection active, the client might wait longer, though it will timeout eventually.
    Last edited by skalogir; 2012-02-29 at 15:21.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Hyderabad,AP,India.
    Posts
    62

    Re: HTTP Connection sending request twice after 30 sec

    Hi skalogir,

    Unfortunately none of the methods described by you did not work for me.
    did any one found a solution for this issue??
    Regards,
    Harsha

  6. #6
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,087

    Re: HTTP Connection sending request twice after 30 sec

    Harsha - a bit of change on the server side can help you, you need to start writing the Output Stream from the server within 30 sec's of the hit - don't wait on the server side to compile all data and then send back to the client... keep writing back in chunks... on the stream..to keep the connection live... and avoid this auto-gen retry.

    Ad a timestamp or unique stamp to every request which you create on the device side... keep a track of these on the server side to avoid processing of these duplicate requests that be ignored.
    thanks,
    ~Amitabh
    (Champion of the Month -Aug'13)
    Follow me on my blog for Innovative Mobile Apps

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Hyderabad,AP,India.
    Posts
    62

    Re: HTTP Connection sending request twice after 30 sec

    Hi im2amit,

    As you said "keep writing back in chunks..." is not possible for me because , my application is for mobile recharge process, which has to wait for the process to be completed.
    Im always getting a response for the second request as fail, but eventually it is a success in the background for the first request itself. so, I think keeping a timestamp and ignoring the request wouldn't help much because showing a fail response for a successful one has no meaning.

    Note: Im using PHP for server side.
    Regards,
    Harsha

  8. #8
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,087

    Re: HTTP Connection sending request twice after 30 sec

    Ignore the second request, do not give any response if you find the stamp same as 1st which is still under process... and once the process is complete - you respond as a success.... which you will get on the device side J2me code.
    thanks,
    ~Amitabh
    (Champion of the Month -Aug'13)
    Follow me on my blog for Innovative Mobile Apps

  9. #9
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,771

    Re: HTTP Connection sending request twice after 30 sec

    Quote Originally Posted by harsha921 View Post
    As you said "keep writing back in chunks..." is not possible for me because , my application is for mobile recharge process, which has to wait for the process to be completed.
    You can certainly send some "application-layer keepalive" message. Like writing "PleaseWait" into the stream every 20 seconds until the real content is ready. And of course the client should silently ignore these "PleaseWait" parts.
    A possible downside is that you can not use HTTP error codes with this approach, even if you wanted to.

Similar Threads

  1. Sending multiple http get request
    By dev_Cis in forum Symbian Networking & Messaging (Closed)
    Replies: 7
    Last Post: 2011-12-16, 07:48
  2. S40 Phones sending HTTP request twice
    By gaurav007 in forum Mobile Java Networking & Messaging & Security
    Replies: 3
    Last Post: 2010-01-04, 13:11
  3. Sending minimum Headers in HTTP request
    By KevinBoyd in forum Mobile Java General
    Replies: 1
    Last Post: 2009-09-20, 10:20
  4. Sending HTTP GET request with body
    By moldovan_catalin in forum Symbian Networking & Messaging (Closed)
    Replies: 2
    Last Post: 2009-01-13, 19:49
  5. problem sending an http request with # in it.
    By jaazz in forum Mobile Java Networking & Messaging & Security
    Replies: 4
    Last Post: 2006-12-06, 11:58

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×