简体   繁体   中英

Sending an http post request to a login website? (Authorization)

I have two classes:

public class ProtectedAuthorizer extends Authenticator {

public String authorizeProtectedUrl(String requestingUrl) {

    Authenticator.setDefault(new CustomAuthenticator());

    StringBuffer sb = null;

    try {
        URL url = new URL(requestingUrl);
        BufferedReader br = new BufferedReader(new InputStreamReader(
                url.openStream()));

        String in = "";
        sb = new StringBuffer();

        while ((in = br.readLine()) != null) {
            sb.append(in + "\n");
        }
        br.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}
}

And this is my second class:

    protected PasswordAuthentication getPasswordAuthentication() {

    String username = "username";
    String password = "password";

    String promptString = getRequestingPrompt();
    String hostname = getRequestingHost();
    InetAddress ipaddr = getRequestingSite();
    int port = getRequestingPort();

    System.out.println(promptString);
    System.out.println(hostname);
    System.out.println(ipaddr);
    System.out.println(port);

    return new PasswordAuthentication(username, password.toCharArray());
}

}

I'm trying to authorize myself to this website: https://id.ogplanet.com/login.og but whenever I call

authorizeProtectedUrl("https://id.ogplanet.com/login.og"); 

it doesn't do anything. How do I use these 2 classes to authorize myself to a login page? (Send an HTTP Post Request to the login page).

For doing HTTP related stuff, I recommend to use the Apache HTTP Components Client library. On the project web site, there a some examples . Look at the Form based logon example . It seems to me, that it matches your use case very well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM