views:

328

answers:

4

We have next code.
Sometimes we should wait 10-20-40 seconds on the last line.
What can be the problem?

Java 1.4

URL url = ...;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.connect();
OutputStream out = conn.getOutputStream();
ObjectOutputStream outStream = new ObjectOutputStream(out);
try
{
   outStream.writeObject(objArray);
}
finally
{
   outStream.close();
}

InputStream input = conn.getInputStream();

UPDATED:
Next code fixes the problem IN ECLIPSE.
But it still DOES NOT WORK via Java WebStart:(

HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
conn.setDoInput(true);  
conn.setDoOutput(true);  
conn.setUseCaches(false);  
System.setProperty("http.keepAlive", "false");  //<---------------
conn.connect();

But why?

UPDATED one more time!
Bug was fixed! :)

We worked with connections not in one class but in two.
And there is following line in the second class:

URL url = ...  
HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
conn.setRequestProperty("Content-Length", "1000");  //<------------
conn.connect();

setRequestProperty("Content-Length", "1000") is root cause of the problem.

A: 

Try it with an IP address. To see if it's a DNS problem.

Thomas Jung
We jave this problem on different computers and in different countries :(
Vladimir Bezugliy
Did you have a look at it with Wireshark?
Thomas Jung
A: 

The problem can be something from network sub layer... Should be hard to find it.

But what about the setReadTimeOut() with low value and a while loop?

enguerran
We use java 1.4
Vladimir Bezugliy
A: 

One thing I would guess is that your DNS server isn't responding well.

Can you experiment with changing symbolic domain names to numeric IP addresses before you start? Or can you do each request twice (just for experimentation) and see if the first request is significantly slower than the second?

Google has put up a DNS server at (among others) 8.8.8.8 . They claim it's faster than most other DNS servers. Give that a try!

Carl Smotricz
We jave this problem on different computers and in different countries :(
Vladimir Bezugliy
In the same company? May the DNS the same within your private network?
enguerran
No - different companies, different DNS, different networks.
Vladimir Bezugliy
It's not so much a matter of where you do the DNS lookup as which DNS server handles the destination. If you're accessing a site in Timbuktu with a slow network and so on, the problem could be there. I wouldn't dismiss this possibility without first having looked at response times for DNS lookups.
Carl Smotricz
Production and test environments are different.
Vladimir Bezugliy
I give up - I can't help you.
Carl Smotricz
+2  A: 

'We had a similar issue which is caused by buggy keep-alive in old Java. Add this before connect to see if it helps,

conn.setRequestProperty("Connection", "close");

or

System.setProperty("http.keepAlive", "false");
ZZ Coder
Did not help :(
Vladimir Bezugliy
How about the 2nd approach? You really need to post a TCP dump to see exactly what the issue is. I don't think it's DNS though because which occurs in connect() call.
ZZ Coder
It seems to me that keepAlive=false helps. But why?
Vladimir Bezugliy
Without a trace, I am just guessing. The HTTP request has keepalive but no content-length, the server doesn't know where is the end of the request so it keeps waiting. The default timeout on Apache is 30 seconds. When keepalive is turned off, the OutputStream will be closed, which notifies the server request is done.
ZZ Coder
wireshark/ethereal, tcpdump, snoop. It really depends on your OS.
ZZ Coder
Setting the `connection=close` header should work. Maybe you didn't set the request property correctly. It should be done prior to `connect()`. Setting the system property `http.keepalive=false` does basically the same, but then as a low-level workaround. When not specified manually, the `URLConnection` uses it to set the `connection` header. That it doesn't work in webstart is simply a security restriction. But anyway, the `setRequestProperty("Connection", "close")` must work. Try again and verify if you did anything right.
BalusC
Uffff! Bug was fixed. See update from me.
Vladimir Bezugliy