Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

multiple http connections in series  RSS feed

 
Tom Griffith
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. If anybody has a minute or whatever, I'm kinda messing around with this and trying to determine the most optimum way of doing it...

I am iterating an array list of file names...they all reside on the same server and accessed via httpurlconnection...i want to open an inputstream to each one of them in sequence...

here's a brief schematic...

URL url;
URLConnection connection;
HttpURLConnection hc;
InputStream is;

for (int i=0; i < file_name_array.size(); i++) {

url=new URL("http://www.blah.org/file_dir/" + file_name_array.get(i));
connection = url.openConnection();

hc = (HttpURLConnection)(connection);
hc.setRequestMethod( "GET" );

is=hc.getInputStream();

is.close();
hc.disconnect();

}

Something seems weird to me...like somehow this could persist a connection to the root url of the server and leveraging that connection when the file name is appended for each iteration...as opposed to connecting/disconnecting each httpurlconnection. Does anybody have a suggestion? I'll keep messing with it. Thank you.
[ December 05, 2006: Message edited by: Tom Griffith ]
 
Joe Ess
Bartender
Posts: 9425
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is necessary to create a new HttpURLConnection for each web resource because HttpURLConnection is an abstraction of an individual request. As long as you don't call HttpURLConnection.disconnect(), the underlaying sockets will maintain a connection so there's no network overhead to creating multiple HttpURLConnection instances.
 
Tom Griffith
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I was perusing the HttpURLCOnnection specs earlier and it seemed a little vague on whether calling disconnect() was of benefit or not. Now it makes sense with respect to the underlying network connections being transparently shared. Thank you.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may find it worth putting these into threads if overall elapsed time matters. I have a little downloader program that runs three or four threads at the same bytes-per-second as one thread. (Over four the benefit is not so good.) It runs 100% CPU but I figure that's A Good Thing because I just want it to hurry up and finish.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!