Using Java to download a file that needs authentication

You can easily download files using Java by making a URLConnection. However, if you need to login before accessing the file, how can do this automatically? If you automate the login, how will the code that makes the URLConnection be able to make use of the logged in session? Well, if the logged in session uses a cookie, you can simply extract the cookie and resend it in your code that makes the URLConnection.  Here is an example where I’ve used the Selenium web testing tool to do the login. The code is actually Scala, but you can easily convert it to Java:

  def getFileViaSelenium() {
    println("Logging in via Selenium")
	  val driver = new FirefoxDriver()
	  driver.findElement("username")).sendKeys("John Smith");
 // now get the cookies	 
   val seleniumCookies = driver.manage().getCookies().asScala
	  val cookieString = new StringBuilder()
	  for (cookie <- seleniumCookies) {
	    println("Cookie value: " + cookie.getValue())
	    cookieString.append("; ")
     println("Getting file")
    val url = new URL("")
    val con = url.openConnection()
  // resend the cookies with this request
    val contentType = con.getContentType()
    println("Content type: " + contentType)
    val in = con.getInputStream()
    // save file
    val fileOut = new File("C:\\my_download.csv")
    println("File saved")
This entry was posted in Java, Scala, Selenium and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

505,038 Spambots Blocked by Simple Comments

HTML tags are not allowed.