Create a URLConnection that can pass username and password in JAAS Security

16,349

Instead of "Proxy-Authorization", try "Authorization". The "Proxy-Authorization" header is used when authenticating against a proxy rather than the actual web server you are trying to access.

Share:
16,349
Admin
Author by

Admin

Updated on June 14, 2022

Comments

  • Admin
    Admin about 2 years

    Hi I have something like the following that makes a URL Connection and retrieves the data. The problem is that when I hit a page that requires authentication first I get the login page. I'm unsure of how to pass a username and password in the URLConnection. I'm hitting a site that is using JAAS authenticaiton.

    import java.net.URL;
    import java.net.URLConnection;
    
    .....
    
    URL location = new URL("www.sampleURL.com");
    URLConnection yc = location.openConnection();
    BufferedReader in = new BufferedReader(
        new InputStreamReader(
        yc.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) 
        data += inputLine + "\n";
    in.close();
    

    I tried something like this but it didn't seem to work...

    URL url = new URL("www.myURL.com");
    URLConnection connection = url.openConnection();
    String login = "username:password";
    String encodedLogin = new BASE64Encoder().encodeBuffer(login.getBytes());
    connection.setRequestProperty("Proxy-Authorization", "Basic " + encodedLogin);