Java
Chilkat2 Java
HTTP Basic Authentication Test
Demonstrates how to do HTTP basic authentication using Chilkat.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
public class ChilkatExample {
public static void main(String[] args) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Http http = new Http();
// To use HTTP Basic authentication:
http.setLogin("myLogin");
http.setPassword("myPassword");
http.setBasicAuth(true);
// Run the test using this URL with the credentials above.
// (Works while httpbin.org keeps the test endpoint available.)
String jsonResponse;
try {
jsonResponse = http.quickGetStr("https://httpbin.org/basic-auth/myLogin/myPassword");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("Response status code: " + http.getLastStatus());
System.out.println(jsonResponse);
// Output:
// Response status code: 200
// {
// "authenticated": true,
// "user": "myLogin"
// }
}
}