Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4017468
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:51:53+00:00 2026-05-20T09:51:53+00:00

I need to get a file into memory in my app from a secured

  • 0

I need to get a file into memory in my app from a secured web location. I have the URL of the file to capture, but can’t seem to get the security issue resolved. Here’s the code from the Cookbook samples page:

def download(address)
{
    def file = new FileOutputStream(address.tokenize("/")[-1])
    def out = new BufferedOutputStream(file)
    out << new URL(address).openStream()
    out.close()
}

and here’s my “memory” version of the same function which should return a byte array of the file’s contents:

def downloadIntoMem(address)
{  // btw, how frickin powerful is Groovy to do this in 3 lines (or less)
        def out = new ByteArrayOutputStream()
        out << new URL(address).openStream()
        out.toByteArray()
}

When I try this against an unsecured URL (pick any image file you can find on the net), it works just fine. However, if I pick a URL that requires a user/password, no go.

All right, done a bit more work on this. It seems that the Authenticator method does work, but in a round-about way. The first time I access the URL, I get a 302 response with a location to a login server. If I access that location with an Authenticator set, then I get another 302 with a Cookie and the location set back to the original URL. If I then access the original, the download occurs correctly.

So, I have to mimic a browser a bit, but eventually it all works.

Making this a community wiki, so others can add other methods.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:51:54+00:00Added an answer on May 20, 2026 at 9:51 am

    If creds on url doesn’t work, you can use this. It works for Basic Authentication.

    new File(localPath).withOutputStream { out ->
        def url = new URL(remoteUrl).openConnection()
        def remoteAuth = "Basic " + "${user}:${passwd}".bytes.encodeBase64()
        url.setRequestProperty("Authorization", remoteAuth);
        out << url.inputStream
    }
    

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading an XML file into a DataSet and need to get the
I need to get the File version information from an exe file originally written
I need to get a file from sourcesafe database programmatically. Any idea of how
I need a quick easy way to get a string from a file in
I need to get the version information of a DLL file I created in
I need to get the default printer name. I'll be using C# but I
I have a list of files, which need to be read, in chunks, into
I'm working with C#.net developing applications for windows mobile 6, and i need get
I need to get a log of user access to our SQL Server so
I need to get the first and last day of a month in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.