Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

Navigator - Web ServicesI could use some simple java examples of how to login and call W/S

avatar
New Contributor

I am using the java.net and java.URL packages, simple code examples to login and make a service call would be helpful

1 ACCEPTED SOLUTION

avatar
New Contributor

Figured it out....

Used the Authenticator class, see below

 

    public static void main(String[] args) {
        try {
            
                Authenticator.setDefault(new CustomAuthenticator());
                 
                URL url = new URL("http://localhost:7187/api/v1/entities?query=type:file&limit=2&offset=10");
                 
                BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                 
                String line;
                while ((line = in.readLine()) != null) {
                    System.out.println(line);
                }
                in.close();
                 
            }
            catch (MalformedURLException e) {
                System.out.println("Malformed URL: " + e.getMessage());
            }
            catch (IOException e) {
                System.out.println("I/O Error: " + e.getMessage());
            }
             
        }
         
        public static class CustomAuthenticator extends Authenticator {
             
            protected PasswordAuthentication getPasswordAuthentication() {
                 
                String username = "admin";
                String password = "admin";
    
                return new PasswordAuthentication(username, password.toCharArray());
                             
        }
                 
    }

View solution in original post

1 REPLY 1

avatar
New Contributor

Figured it out....

Used the Authenticator class, see below

 

    public static void main(String[] args) {
        try {
            
                Authenticator.setDefault(new CustomAuthenticator());
                 
                URL url = new URL("http://localhost:7187/api/v1/entities?query=type:file&limit=2&offset=10");
                 
                BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                 
                String line;
                while ((line = in.readLine()) != null) {
                    System.out.println(line);
                }
                in.close();
                 
            }
            catch (MalformedURLException e) {
                System.out.println("Malformed URL: " + e.getMessage());
            }
            catch (IOException e) {
                System.out.println("I/O Error: " + e.getMessage());
            }
             
        }
         
        public static class CustomAuthenticator extends Authenticator {
             
            protected PasswordAuthentication getPasswordAuthentication() {
                 
                String username = "admin";
                String password = "admin";
    
                return new PasswordAuthentication(username, password.toCharArray());
                             
        }
                 
    }