open URL connection very slow

- I use this code to get website content, and my problem is:

+ when i use it first, it starting very slow, but a second, third....., it starting very fast.

+ i want know how?, why? and the way to solve it.

Thanks.

Here is my code:

import java.io.*;

import java.net.*;

publicclass IOTool{

staticboolean use_proxy =false;

static Proxy proxy;

publicstatic String get(String str_url){

try{

URL url =new URL(str_url);

URLConnection urlCon;

try{

urlCon = url.openConnection();

BufferedReader r =new BufferedReader(new InputStreamReader(urlCon.getInputStream(),"UTF-8"));

StringBuffer raw =new StringBuffer();

char[] buffer =newchar[8192];

int len;

while((len = r.read(buffer)) != -1){

raw.append(buffer, 0, len);

}

r.close();

return raw.toString();

}catch(IOException e){

e.printStackTrace();

}

}catch(MalformedURLException e){

e.printStackTrace();

}

returnnull;

}

}

Message was edited by:

NhTin

[2366 byte] By [NhTina] at [2007-11-26 19:59:29]
# 1

The first time you're probably waiting for a long time to read the EOF because the server is keep-aliving the connection. You can control that by setting the 'Connection' property of the URLConnection to 'close'.

The second and subsequent times you're probably reading from an intermediate HTTP cache.

ejpa at 2007-7-9 22:56:12 > top of Java-index,Core,Core APIs...
# 2
- First i get A link, very slow, and second i get B link and it starting very fast- Cache?
NhTina at 2007-7-9 22:56:12 > top of Java-index,Core,Core APIs...
# 3
Well, what is "first"? First after reboot, first after application start or something else?Also link A and link B - do they have same host name or no?
Michael.Nazarov@sun.coma at 2007-7-9 22:56:12 > top of Java-index,Core,Core APIs...
# 4
ops, sorry.- "First" mean after application start.- And A & B link (sample)+ A : http://google.com+ B: http://forum.java.sun.com
NhTina at 2007-7-9 22:56:12 > top of Java-index,Core,Core APIs...
# 5
Looks strange. Do you use proxy?Does browser open links same way? First - slow, second - fast?
Michael.Nazarov@sun.coma at 2007-7-9 22:56:12 > top of Java-index,Core,Core APIs...
# 6

i don't use proxy, i turn off firewall, my browser load two that links in normal

i tried reverse two link or change it with new link, but this problem was repeated

first slow

second, third.... very fast

when the first link was invoke, it seem be take a long time to init some things? at this time no byte was sended or received, after that, it get first link content (fast)

with second link, all thing happen very fast. no wait time.

NhTina at 2007-7-9 22:56:13 > top of Java-index,Core,Core APIs...
# 7
Well, some suggestions:try to add few logging lines (with timestamps) into your code to find which code is providing delay.Try to reach local URLs. Do you have HTTP servers accessible by 127.0.0.1?May be omitted parts of your code contains some issues caused delay?
Michael.Nazarov@sun.coma at 2007-7-9 22:56:13 > top of Java-index,Core,Core APIs...