From the errorneous response which appears to be just a HTML error page:
alert('Session timed out. Please login again.\n');
It thus appears that downloading the PDF file is required to take place in a valid HTTP session. The HTTP session is backed by a cookie. The HTTP session in turn contains in the server side usually information about the currenty active and/or logged-in user.
The Selenium web driver manages cookies all by itself fully transparently. You can obtain them programmatically as follows:
Set<Cookie> cookies = driver.manage().getCookies();
When manually fiddling with java.net.URL
outside control of Selenium, you should be making sure yourself that the URL connection is using the same cookies (and thus also maintaining the same HTTP session). You can set cookies on the URL connection as follows:
URLConnection connection = new URL(driver.getCurrentUrl()).openConnection();
for (Cookie cookie : driver.manage().getCookies()) {
String cookieHeader = cookie.getName() + "=" + cookie.getValue();
connection.addRequestProperty("Cookie", cookieHeader);
}
InputStream input = connection.getInputStream(); // Write this to file.