How can I do several request in one HttpURLConnection with Java?
URL url = new URL("http://my.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
HttpURLConnection.setFollowRedirects( true );
connection.setDoOutput( true );
connection.setRequestMethod("GET");
PrintStream ps = new PrintStream( connection.getOutputStream() );
ps.print(params);
ps.close();
connection.connect();
//TODO: do next request with other url, but in same connection
Thanks.
From the Javadoc:
Each HttpURLConnection instance is used to make a single request.
The object apparently isn't meant to be re-used.
Aside from a little memory thrashing and inefficiency, there's no big problem with opening one HttpURLConnection for every request you want to make. If you want efficient network IO on a larger scale, though, you're better off using a specialized library like Apache HttpClient.