Re: Speeding up URLConnection




<mark13.pl@xxxxxxxxx> wrote in message news:1156946176.878142.50630@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hello,

I want to save as a string html file. My code looks like that:

URL url = new URL(fileName);
URLConnection conn = url.openConnection();
conn.setRequestProperty("Cookie", myCookieCode);
conn.connect();
BufferedReader dis = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
String inputLine = null;
for(;;) {
inputLine = dis.readLine();
htmlCode += inputLine;
if (inputLine == null) break;
}

It works perfectly (in htmlCode I have the whole page as I wanted) but
it has very big disadvantage - it is VERY slow. In both my browsers
(Firefox, IE6.0, cache cleaned) it takes about 2seconds to load it
while in java: about 14seconds. Do you know where is a problem and how
can I speed it up??

If you have a profiler, you should use it to measure where your code is spending all your time. However, from a quick glance, my guess is that the slowestp art is string concatenation. Use a StringBuilder instead:

URL url = new URL(fileName);
URLConnection conn = url.openConnection();
conn.setRequestProperty("Cookie", myCookieCode);
conn.connect();
BufferedReader dis = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder tempHtmlCode = new StringBuilder();
do {
String inputLine = dis.readLine();
tempHtmlCode.append(inputLine);
} while (inputLine != null)
htmlCode = tempHtmlCode.toString();

- Oliver

.



Relevant Pages

  • Problem in Java class while using URL class
    ... Exception in thread "main" java.net.UnknownHostException: ... URL url = new URL; ... String line; ...
    (comp.lang.java.programmer)
  • Re: get ResponseCode() and connect()
    ... connect then get the response code of the remote server. ... URL url = new URL; ... String name = uc.getHeaderFieldKey; ...
    (comp.lang.java.programmer)
  • Re: Http to port different than 80?
    ... > response according to the myXMLQuery. ... My guess is that your XML query string contains characters that need to be ... URL url = new URL; ...
    (comp.lang.java.databases)
  • speeding up URLConnection reading
    ... I want to read the content of some webpages and make some string ... URL url = new URL; ... inputLine = dis.readLine; ...
    (comp.lang.java.programmer)
  • Re: speeding up URLConnection reading
    ... I want to read the content of some webpages and make some string ... URL url = new URL; ... final HttpURLConnection connection = ... final Reader reader = new InputStreamReader; ...
    (comp.lang.java.programmer)