javaeclipsesoapwsdldhl

Java WSDL DHL Classes


I have create Java Classes from the DHL WSDL https://cig.dhl.de/cig-wsdls/com/dpdhl/wsdl/geschaeftskundenversand-api/2.2/geschaeftskundenversand-api-2.2.wsdl.

Now i have all Classes, but no Authentifaction Class. I try this

 GKVAPIServicePortTypeProxy port2 = new GKVAPIServicePortTypeProxy();
        port2.setEndpoint("https://cig.dhl.de/services/sandbox/soap");

     CreateShipmentOrderRequest sh = new CreateShipmentOrderRequest();
        //Setting up shipment;
        .. and so on

        CreateShipmentOrderResponse chr = port2.createShipmentOrder(sh);

But only what i get is, "(401)Authorization Required" How can i set my Authentifiaction ?


Solution

  • Using basic authentication, you would first Base64 encode your username:password - there's online sites that will do it but beware as likely not a good idea to do it if it refers to DHL in anyway, e.g. they could swipe your credentials.
    You then get the Request Context of the port, create a map of the headers and add an Authorization header. Finally you add that back to the request context.

    Example: Note, I purposely generated bad base64 encoding so you would likely not be able to decode it and see it properly formatted with the "username:password"

            GVAPI20De service1 = new GVAPI20De();
    
            GKVAPIServicePortType port2 = service1.getGKVAPISOAP11Port0();
    
            CreateShipmentOrderRequest sh = new CreateShipmentOrderRequest();
            //Setting up shipment;
    
            Map<String, Object> req_ctx = ((BindingProvider)port2).getRequestContext();
    
            //you may not need this and can try commenting it out
            req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "https://cig.dhl.de/cig-wsdls/com/dpdhl/wsdl/geschaeftskundenversand-api/2.2/geschaeftskundenversand-api-2.2.wsdl");
    
            //optional timeout
            req_ctx.put("javax.xml.ws.client.connectionTimeout", "60000");
    
            Map<String, List<String>> headers = new HashMap<String, List<String>>();
            headers.put("Authorization", Collections.singletonList("Basic c3gh567sd4689k11lg=="));
    
            req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
    
            CreateShipmentOrderResponse chr = port2.createShipmentOrder(sh)