我已经从DHL WSDL创建Java类 https://cig.dhl.de/cig-wsdls/com/dpdhl/wsdl/geschaeftskundenversand-api/2.2/geschaeftskundenversand-api-2.2.wsdl。
现在我拥有所有班级,但是没有身份验证班级。 我试试这个
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);
但只有我得到的是“(401)需要授权” 如何设置我的身份验证?
答案 0 :(得分:0)
使用基本身份验证,您首先要对您的用户名:密码进行Base64编码-有在线站点可以进行此操作,但是请注意,如果它无论如何都引用DHL,则最好不要这样做,例如他们可以刷您的凭据。
然后,您将获得端口的请求上下文,创建头的映射并添加授权头。最后,将其添加回请求上下文。
示例:
请注意,我故意生成了错误的base64编码,因此您可能无法对其进行解码,并无法使用“ 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)
答案 1 :(得分:0)
您好,我通过将DHL的ssl证书添加到我的应用程序信任库中来解决401问题。 但是我有一个问题,我想将Authentification块添加到请求中。
<soapenv:Header>
<cis:Authentification>
<cis:user>user</cis:user>
<cis:signature>password</cis:signature>
</cis:Authentification>
</soapenv:Header>
我尝试添加此块,从而导致'org.quartz.jobexecutionexception:de.vps.icms.exceptions.icmsscriptingexception:java.lang.noclassdeffounderror:org / apache / axis2 / saaj / soapenvelopeimpl'exception。
有些想法我做错了什么? 这里的代码: 公共类WSClient {
public WSClient() {
try {
GKVAPIServicePortType port = prepareService();
String b = BWIConstants.SYSPARAM_DHL_WS_URL;
CreateShipmentOrderRequest createShipmentOrderRequest = new CreateShipmentOrderRequest();
CreateShipmentOrderResponse createShipmentOrderResponse =
port.createShipmentOrder(createShipmentOrderRequest);
createShipmentOrderResponse.getStatus();
} catch (Exception e) {
e.printStackTrace();
}
}
private GKVAPIServicePortType prepareService() throws MalformedURLException {
// get Service stub
String pathToClassFolder = getClass().getResource("/").toString();
String fullwsdlFilePath = pathToClassFolder + "/" + "geschaeftskundenversand-api-2.2.wsdl";
URL wsdlLocation = new URL(fullwsdlFilePath);
GVAPI20De service = new GVAPI20De(wsdlLocation);
// get Service Port
GKVAPIServicePortType port = service.getPort(GKVAPIServicePortType.class);
// overwrite Endpoint
Map<String, Object> requestContext = ((BindingProvider) port).getRequestContext();
requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "https://cig.dhl.de/services/sandbox/soap");
// overwrite BasicAuth Username and Password
// requestContext.put(BindingProvider.USERNAME_PROPERTY, cigUser);
// requestContext.put(BindingProvider.PASSWORD_PROPERTY, cigPass);
// Add authentication Handler
Binding binding = ((BindingProvider) port).getBinding();
List<Handler> handlerChain = binding.getHandlerChain();
handlerChain.add(
new AuthenticationHandler(BWIConstants.SYSPARAM_DHL_WS_USER, BWIConstants.SYSPARAM_DHL_WS_SIGNATURE));
binding.setHandlerChain(handlerChain);
return port;
}
}
public class AuthenticationHandler implements SOAPHandler<SOAPMessageContext> {
private String USER = "";
private String PASSWORD = "";
public AuthenticationHandler(final String user, final String password) {
USER = user;
PASSWORD = password;
}
/**
* {@inheritDoc}
*/
public void close(final MessageContext context) {
// nothing to do
}
/**
* {@inheritDoc}
*/
public Set<QName> getHeaders() {
// nothing to do
return null;
}
/**
* {@inheritDoc}
*/
public boolean handleFault(final SOAPMessageContext context) {
// nothing to do
return true;
}
/**
* {@inheritDoc}
*/
public boolean handleMessage(final SOAPMessageContext context) {
if (isOutboundMessage(context)) {
try {
// get/create the map of HTTP headers
Map<Object, Object> headers = (Map<Object, Object>) context.get(MessageContext.HTTP_REQUEST_HEADERS);
if (headers == null) {
headers = new HashMap<Object, Object>();
context.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
}
// add custom HTTP header (deactivate HTTP keepAlive)
String headerName = "Connection";
List<String> headerValues = new ArrayList<String>();
headerValues.add("Close");
headers.put(headerName, headerValues);
SOAPMessage message = context.getMessage();
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
SOAPHeader header;
if (envelope.getHeader() == null) {
header = envelope.addHeader();
} else {
header = envelope.getHeader();
}
// add the Authentification element
SOAPElement auth = header.addHeaderElement(
envelope.createName("Authentification", "cis", "http://dhl.de/webservice/cisbase"));
SOAPElement user =
auth.addChildElement(envelope.createName("user", "cis", "http://dhl.de/webservice/cisbase"));
user.setValue(USER);
SOAPElement signature =
auth.addChildElement(envelope.createName("signature", "cis", "http://dhl.de/webservice/cisbase"));
signature.setValue(PASSWORD);
SOAPElement type =
auth.addChildElement(envelope.createName("type", "cis", "http://dhl.de/webservice/cisbase"));
type.setValue("0");
// save changes
message.saveChanges();
} catch (SOAPException ex) {
throw new RuntimeException("Failed to add SOAP headers for authentication.", ex);
}
}
return true;
}
private boolean isOutboundMessage(final MessageContext context) {
Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
return outboundProperty.booleanValue();
}
}