Java Basic Auth

SOLVED

Does anybody have any examples of calling X3 web services in V12, particularly the basic auth portion of the call?  The documentation for V9 and later only includes .net, which I already have working.

  • +1
    verified answer

    It really depends on which library you are using to create http call. 

    You need to add "Authorization", "Basic"  property to http header. (Ex. Authorization: Basic dXNlcjE6dXNlcjFQYXNz)

    Some libraries have functions to add it as username/password, or you can manually modify the header.

    create "user:password" - encode it with base64 - create Authorization property with Basic + encoded string.

    This is how I did it for ksoap2 on Android, and I have tested it with v12:

    String basicAuthName = "web";
    String basicAuthPass = "web";
    byte[] token = (basicAuthName + ":" + basicAuthPass).getBytes();
    headerList.add(new HeaderProperty("Authorization", "Basic " + org.kobjects.base64.Base64.encode(token)));


    This is an example for httpclient (org.apache.http) on Java. It should work the same way:

    HttpGet request = new HttpGet(URL_SECURED_BY_BASIC_AUTHENTICATION);

    String auth = DEFAULT_USER + ":" + DEFAULT_PASS;
    byte[] encodedAuth = Base64.encodeBase64(
      auth.getBytes(StandardCharsets.ISO_8859_1));
    String authHeader = "Basic " + new String(encodedAuth);
    request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
    HttpClient client = HttpClientBuilder.create().build();
    HttpResponse response = client.execute(request);
    And after this handle the SOAP/Rest request normally.
  • 0 in reply to SAV

    Thank you.  We did eventually figure it out, but thanks for your response.

  • 0 in reply to Denise Hartman

    Hi, i have some problem with jAVA. Do you have a solution ?