Java
Chilkat2 Java
Configure HTTP Basic Authentication with SecureString
See more REST Examples
Demonstrates Rest.SetAuthBasicSecure, which configures HTTP Basic authentication using SecureString objects instead of plaintext strings.
Background. A SecureString keeps sensitive values encrypted in memory, reducing the window in which credentials appear as readable plaintext. This is otherwise equivalent to SetAuthBasic.
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Rest;
import com.chilkatsoft.chilkat2.SecureString;
public class ChilkatExample {
public static void main(String[] args) {
Rest rest = new Rest();
boolean bTls = true;
boolean bAutoReconnect = true;
try {
rest.connect("example.com", 443, bTls, bAutoReconnect);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// SetAuthBasicSecure configures HTTP Basic authentication using SecureString objects, which keep
// the credentials encrypted in memory.
// Normally you would not hard-code secrets in source. You should instead obtain them from an
// interactive prompt, environment variable, or a secrets vault.
SecureString ssUsername = new SecureString();
ssUsername.append("myUsername");
SecureString ssPassword = new SecureString();
ssPassword.append("myPassword");
String responseText;
try {
rest.setAuthBasicSecure(ssUsername, ssPassword);
responseText = rest.fullRequestNoBody("GET", "/api/protected");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println(responseText);
}
}