• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

slowness in HttpURLConnection?

 
Prateek Sharma
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I am using a small code for downloading a web page content. But its working very slow. When I open same web page using my mozilla browser it hardly takes 2-3 seconds but when I use my Java code it takes 55-60 seconds for the same page (Just for HTML, no images). What could be the reason.





Thanks in advance
 
Joe Ess
Bartender
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is some fairly inefficient code. While you do use a BufferedInputStream, which is good, you read it one. byte. at. a. time. Which is not good.
Furthermore, you have a Reader, which does character conversion, but you since you are reading it by the byte, you have to do a second character conversion (the cast of 'c' to char).
 
Prateek Sharma
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe,

Could you please suggest me what is the better way to do it ....

Regards
-
P
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should read more than a single character at a time. See http://faq.javaranch.com/java/ReadDoesntDoWhatYouThinkItDoes for an example of that.

Also note that String.valueOf uses the platform default encoding for the conversion - which may not be the right one. You need to check the Content-Type header of the response to find out which encoding it's in, and then use that for your byte-to-String conversion.
 
Joe Ess
Bartender
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I wrote that FAQ
I'd recommend following the link at the bottom of the FAQ to the Java Platform Performance book. There' s more than one way to buffer IO and some methods make more sense in some situations.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic