Link to home
Start Free TrialLog in
Avatar of cindymccartney
cindymccartneyFlag for United States of America

asked on

How do I connect to an https URL to download a file?

I am writing a java program to download a daily file from an https url.
i.e. https://example.net/Downloads/guid.zip

I have a truststore and keystore configured correctly.
I have also been given a username and password.

My problem at this point is with the connection.  When I attempt to connect, I get the following error:
Access denied (java.util.PropertyPermission javax.net.ssl.keyStore write)

I'm not exactly sure where to incorporate my username and password. Also, not sure what the setRequestProperty to look like.   My code is below.

System.setProperty( "javax.net.ssl.keyStore", "/is/domains/domain48000/config/clientkeystore.jks" );
            System.setProperty( "javax.net.ssl.keyStorePassword", "kspass" );
            System.setProperty( "javax.net.ssl.trustStore", "/is/domains/domain48000/config/cacerts.jks" );
            System.setProperty( "javax.net.ssl.trustStorePassword", "tspass" );
            java.net.URL url = new java.net.URL( https://example.net/Downloads/guid.zip );
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            conn.setDoInput( true );
            conn.setDoOutput( true );
            conn.setUseCaches( false );
            conn.setRequestProperty( "Content-Type", "text/xml" );
            InputStream stream = conn.getInputStream();
            BufferedInputStream in = new BufferedInputStream( stream );
            FileOutputStream file = new FileOutputStream( "/home/myFile.zip" );
            BufferedOutputStream out = new BufferedOutputStream( file );
            int i;
            while ((i = in.read()) != -1) {
                out.write( i );
            }
            out.flush();

Open in new window

Avatar of Bryan Butler
Bryan Butler
Flag of United States of America image

So is the error happening on line 2 then?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Firstly install Openssl to support HTTPS communication. Download OpenSSL exe from web and install, then can use below code.
import java.io.*;
import java.net.*;
 
class Test{
    
    public void getZip(String link, String file) throws Exception{
        URL url = new URL(link);
        URLConnection urlConnection = url.openConnection();
        DataInputStream dis = new DataInputStream(urlConnection.getInputStream());
        DataOutputStream out = new DataOutputStream(new FileOutputStream( file  ) );
        
        int i;
        while ((i = dis.read()) != -1) {
            out.write( i );
        }
        out.flush();
        dis.close();
    }
    
    public static void main(String []argv) throws Exception{
        new Test().getZip("https://code.bandit-project.org/trac/export/3/trunk/RoleEngine/doc/rejavadoc.zip", "out.zip");
    }
}

Open in new window

Avatar of cindymccartney

ASKER

CEHJ -

I attempted to your solution, but am having a problem.  I created a new package to hold the MyAuthenticator class and imported that package into my main java program.  

The first line in my code (as per the example) is this:

java.net.Authenticator.setDefault( new com.dva.Authenticator.MyAuthenticator() );

However, I am getting a "class com.dva.Authenticator.MyAuthenticator() cannot be resolved".
It seems it doesn't like "MyAuthenticator()" when attempted to create the object.

Below is the java file that I put into a package called com.dva.Authenticator:



package com.dva.Authenticator;
 
import java.net.*;
 
    public class MyAuthenticator extends Authenticator {
        // This method is called when a password-protected URL is accessed
        protected PasswordAuthentication getPasswordAuthentication() {
            // Get information about the request
            String promptString = getRequestingPrompt();
            String hostname = getRequestingHost();
            InetAddress ipaddr = getRequestingSite();
            int port = getRequestingPort();
 
            // Get the username from the user...
            String username = "myusername";
 
            // Get the password from the user...
            String password = "mypassword";
 
            // Return the information
            return new PasswordAuthentication(username, password.toCharArray());
        }
    }

Open in new window

ok, just an update.  I got the classes to work correctly per CEHJ's example.  

However, I am getting a different error:

Access denied (java.net.NetPermission setDefaultAuthenticator)|#]
You've got a SecurityManager in place? Are you doing this inside a servlet container?
I'm writing the code inside Sun's JCAPS application.

I'm not sure if there is a SecurityMangager in place or not.

I'm also not sure if the java collaboration inside of JCAPS is considered a servlet container.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks CEHJ for your continued help.

I do have a java.policy file (pasted below)  in one of the application server directories.  Do I need to add a permission entry into this file?

If so, can you give me an example of what it would look like?

Or maybe I'm not on the right track at all.

I'm not explicitly enabling the security manager, but it might be something the App server is enabling by default somehow.  Otherwise, how would I go about disabling it?

//
// @(#)src/security/sov/config/java.policy, security, asdev, 20050521 1.4
// ===========================================================================
// Licensed Materials - Property of IBM
// "Restricted Materials of IBM"
//
// IBM SDK, Java(tm) 2 Technology Edition, v1.4.2
// (C) Copyright IBM Corp. 1998, 2002. All Rights Reserved
// ===========================================================================
//
 
 
// Standard extensions get all permissions by default
 
grant codeBase "file:${java.home}/lib/ext/*" {
        permission java.security.AllPermission;
};
 
// default permissions granted to all domains
 
grant { 
        // Allows any thread to stop itself using the java.lang.Thread.stop()
        // method that takes no argument.
        // Note that this permission is granted by default only to remain
        // backwards compatible.
        // It is strongly recommended that you either remove this permission
        // from this policy file or further restrict it to code sources
        // that you specify, because Thread.stop() is potentially unsafe.
        // See "http://java.sun.com/notes" for more information.
        permission java.lang.RuntimePermission "stopThread";
 
        // allows anyone to listen on un-privileged ports
        permission java.net.SocketPermission "localhost:1024-", "listen";
 
        // "standard" properies that can be read by anyone
 
        permission java.util.PropertyPermission "java.version", "read";
        permission java.util.PropertyPermission "java.vendor", "read";
        permission java.util.PropertyPermission "java.vendor.url", "read";
        permission java.util.PropertyPermission "java.class.version", "read";
        permission java.util.PropertyPermission "os.name", "read";
        permission java.util.PropertyPermission "os.version", "read";
        permission java.util.PropertyPermission "os.arch", "read";
        permission java.util.PropertyPermission "file.separator", "read";
        permission java.util.PropertyPermission "path.separator", "read";
        permission java.util.PropertyPermission "line.separator", "read";
 
        permission java.util.PropertyPermission "java.specification.version", "read";
        permission java.util.PropertyPermission "java.specification.vendor", "read";
        permission java.util.PropertyPermission "java.specification.name", "read";
 
        permission java.util.PropertyPermission "java.vm.specification.version", "read";
        permission java.util.PropertyPermission "java.vm.specification.vendor", "read";
        permission java.util.PropertyPermission "java.vm.specification.name", "read";
        permission java.util.PropertyPermission "java.vm.version", "read";
        permission java.util.PropertyPermission "java.vm.vendor", "read";
        permission java.util.PropertyPermission "java.vm.name", "read";
 
};

Open in new window

Just an update, I was able to get it working.  Here is what I needed to add to my server.policy file:

Replace the following line:
   permission java.util.PropertyPermission "*", "read"; with
   permission java.util.PropertyPermission "*", "read,write";

And then within the same grant clause as the above add the following line:

permission java.net.NetPermission "setDefaultAuthenticator"
Thanks CEHJ for your help!
Good - glad it's working :-)