I was wondering if any of you know how to "log out" with Basic Authentication (BA) using the java.net.Authenticator class. I know that BA does not have a logout method, and you need to close and reopen the browser to end the session. The question is, how do you close and open the browser in Java code? That is, I connect via java, and not to borwser, so how do I get the JVM to “de-authenticate” itself?
Context: I am writing a tweet posting application for multiple Twitter accounts using BA. I can use java.net. * To publish a tweet for one account (and see that it calls my authentication class), but when I try to publish a tweet for the second, I do not see the second call of the authenticator, and the tweet will be launched on the first account.
Is it possible to return the authenticator for re-authentication or is it a dead end? If so, I can just use OAuth instead.
Thanks so much for any information you can offer!
Shane
static class MyAuthenticator extends Authenticator { private String username, password; public MyAuthenticator(String user, String pass) { username = user; password = pass; } protected PasswordAuthentication getPasswordAuthentication() { System.out.println("Requesting Host : " + getRequestingHost()); System.out.println("Requesting Scheme : " + getRequestingScheme()); System.out.println("Requesting Site : " + getRequestingSite()); return new PasswordAuthentication(username, password.toCharArray()); } }
public void tweet (AutoTwitterAccount acc, String tweet) {Authenticator.setDefault (zero);
Authenticator.setDefault(new MyAuthenticator(acc.getUserName(), acc.getPassword())); try { URL url = new URL(AutoTweeter.TWEET_URL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("User-Agent", "Mozilla/4.0"); conn.setRequestProperty("User-Agent", AGENT); conn.setRequestProperty("Content-Type", TYPE); conn.setRequestProperty("Content-Length", "" + tweet.length()); ...fire off tweet.... conn.disconnect();
Thanks again!
source share