HttpURLConnection - file sent twice without asking for

I'm writing a client app, and I use the class java.net.HttpURLConnection to connect to a server (servlet) to send and retreive XML messages.

It works fine on my test box (Linux with Jarkarta tomcat and my perso servlet code to copy the file on the server) on the intranet.

The problem happens when I send this file/message on a server on the internet (port 80).

It seems when the server take too much time (2 mins), my client app resends the XML message again. Then I receive an error message from the server saying I've sent the message twice !

I sniffed my connection, and it looks like my client on the firts sending receives some response after a while (2 mins), then it changes the client port and resends the message again.

Is there someone to explain me that ?

Here is my connection code :

try

{

URL url = new URL(urlstring);

URLConnection conn = url.openConnection();

if (conn==null)

throw new IOException("Can't connect to "+urlstring);

if ( conn instanceof HttpURLConnection)

httpcon = (HttpURLConnection)conn;

else

throw new IOException("http Connection to "+urlstring+" failed:");

httpcon.setRequestProperty("Content-Type", "text/plain" );

httpcon.setDoOutput(true);

httpcon.setDoInput(true);

httpcon.setUseCaches (false);

httpcon.setDefaultUseCaches(false);

}

catch (MalformedURLException e)

{

throw new IOException(e.getMessage());

}

The method I use to write to the server (nothing original) :

httpcon.setRequestMethod("POST");

outputStream = httpcon.getOutputStream();

...

BufferedInputStream in = new BufferedInputStream(inputStream);

OutputStream out = getOutputStream();

byte buff[] = new byte[512];

int len;

try {

while ( (len = in.read(buff, 0, buff.length)) >= 0) {

out.write(buff, 0, len);

}

} catch (java.io.EOFException e) {//REVIEW !!

throw e;

}

Thanks for your help.

[2066 byte] By [Laurent10a] at [2007-9-30 1:53:22]
# 1
I'm doing something similar without problems. The only difference between my code and yours is I do this:hpcon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");Because XML data is pretty icky to send over an URL. Maybe that's your problem?
gimbal2a at 2007-7-16 13:03:54 > top of Java-index,Archived Forums,Socket Programming...
# 2

Thanks, but that's not it.

In fact using this content-type makes things worse : the server answer with the http error 500.

I didn't mention that the XML message is transformed in Base64 before to be sent.

Could it be a setting on my company's firewall/proxy that provokes this double sending ?

On the sniffer tracks I see the server answering my app with something, and then the resending happens.

Laurent1010a at 2007-7-16 13:03:54 > top of Java-index,Archived Forums,Socket Programming...
# 3
Ok, I found the problem with the IS guy.It was on our firewall http proxy : just push the time out to 5 mins and it works fine now !It might help other guys to know...
Laurent1010a at 2007-7-16 13:03:54 > top of Java-index,Archived Forums,Socket Programming...