Pregunta

Estoy haciendo un poco de autenticación HTTP simple y estoy recibiendo un

java.lang.IllegalArgumentException: Illegal character(s) in message header value: Basic OGU0ZTc5ODBk(...trimmed from 76 chars...)
(...more password data...)

que creo que se debe a que yo tenga una muy larga usuario y contraseña y el codificador lo envuelve con una \n a los 76 caracteres. ¿Hay alguna manera de evitar esto? La URL sólo admite autenticación básica de HTTP.

Aquí está mi código:

private class UserPassAuthenticator extends Authenticator {
    String user;
    String pass;
    public UserPassAuthenticator(String user, String pass) {
        this.user = user;
        this.pass = pass;
    }

    // This method is called when a password-protected URL is accessed
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, pass.toCharArray());
    }
}

private String fetch(StoreAccount account, String path) throws IOException {
    Authenticator.setDefault(new UserPassAuthenticator(account.getCredentials().getLogin(), account.getCredentials().getPassword()));

    URL url = new URL("https", account.getStoreUrl().replace("http://", ""), path);
    System.out.println(url);

    URLConnection urlConn = url.openConnection();
    Object o = urlConn.getContent();
    if (!(o instanceof String)) 
        throw new IOException("Wrong Content-Type on " + url.toString());

    // Remove the authenticator back to the default
    Authenticator.setDefault(null);
    return (String) o;
}

Otros consejos

Esto funciona para mí.

HttpsURLConnection con = null; con = (HttpsURLConnection) obj.openConnection (); . Serie de codificación = Base64.getEncoder () encodeToString (: .getBytes "nombre de usuario contraseña" (StandardCharsets.UTF_8)); con.setRequestProperty ( "Autorización", "Basic "+ encoding.replaceAll (" \ n", ""));

He encontrado que el carácter ilegal fue causada por "Autorización: Básica", codificada que debe ser "Autorización", "Basic" + codificada

scroll top