DHL Webservice安全问题

时间:2018-01-01 13:51:38

标签: java web-services wsdl dhl

我尝试连接DHL Webservice searchLocations DHL wsdl https://standorte.deutschepost.de/webservice/?wsdl 在这里我使用的代码(只是试图测试它),我总是得到这个例外:

de.dpag.postfinder.webservice.ServiceException_Exception:拒绝访问。访问密钥无效。

import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.MessageContext;
import de.dpag.postfinder.webservice.AutomatWS;
import de.dpag.postfinder.webservice.InputAddress;
import de.dpag.postfinder.webservice.WebServiceImpl;
import de.dpag.postfinder.webservice.WebServiceImplService;

public class DHLWebService {

  public static void main(String[] args) throws Exception {
    InputAddress request = new InputAddress();

    request.setCountryCode("DE");
    request.setCity("Bonn");
    request.setStreet("harles-de-Gaulle-Str");
    request.setStreetNo("20");
    request.setZip("53113");
    List<AutomatWS> packstationsByAddress = new ArrayList<AutomatWS>();

    String wsdlLocation = "https://standorte.deutschepost.de/webservice/?wsdl";
    try {
      URL wsdlUrl = new URL(wsdlLocation);
      QName qName = new QName("http://postfinder.dpag.de/webservice", "WebServiceImplService");
      WebServiceImplService dhlClientService = new WebServiceImplService(wsdlUrl, qName);
      WebServiceImpl webServiceImplPort = dhlClientService.getWebServiceImplPort();
      Map<String, Object> req_ctx = ((BindingProvider) webServiceImplPort).getRequestContext();
      req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "https://standorte.deutschepost.de/webservice/?wsdl");
      Map<String, List<String>> headers = new HashMap<String, List<String>>();
      //sample username and password
      headers.put("Username", Collections.singletonList("test"));
      headers.put("Password", Collections.singletonList("test"));
      req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);

      packstationsByAddress = webServiceImplPort.getPackstationsByAddress("", request);
    } catch (Exception e) {
      e.printStackTrace();
    }
    for (AutomatWS automatWS : packstationsByAddress) {
      System.out.println(automatWS.getAddress());
    }
  }
}

1 个答案:

答案 0 :(得分:0)

我找出了问题,终点需要在wsdl文件的末尾,如下所示:

<service name="WebServiceImplService">
<port name="WebServiceImplPort" binding="tns:WebServiceImplPortBinding">
<soap:address location="https://cig.dhl.de/services/sandbox/soap" />
</port>
</service>

然后代码应如下所示:

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;

import de.dpag.postfinder.webservice.AutomatWS;
import de.dpag.postfinder.webservice.InputAddress;
import de.dpag.postfinder.webservice.WebServiceImpl;
import de.dpag.postfinder.webservice.WebServiceImplService;

public class DHLWebService {


    //wsdl path
private static final String RELATIVE_WSDL_PATH = "";

public static void main(String[] args) throws Exception {
    InputAddress request = new InputAddress();
    request.setCountryCode("DE");
    request.setCity("Bonn");
    request.setStreet("harles-de-Gaulle-Str");
    request.setStreetNo("20");
    request.setZip("53113");
    List<AutomatWS> packstationsByAddress = new ArrayList<>();
    try {
        URL wsdlLocalURL = DHLWebService.class.getResource(RELATIVE_WSDL_PATH);
      QName qName = new QName("http://postfinder.dpag.de/webservice", "WebServiceImplService");
      WebServiceImplService dhlClientService = new WebServiceImplService(wsdlLocalURL,qName);
      WebServiceImpl webServiceImplPort = dhlClientService.getWebServiceImplPort();
       Map<String, Object> req_ctx = ((BindingProvider) webServiceImplPort).getRequestContext();
       // replace it with your username and password
       req_ctx.put(BindingProvider.USERNAME_PROPERTY, "test");
       req_ctx.put(BindingProvider.PASSWORD_PROPERTY, "test");

      packstationsByAddress = webServiceImplPort.getPackstationsByAddress("", request);
    } catch (Exception e) {
      e.printStackTrace();
    }
    for (AutomatWS automatWS : packstationsByAddress) {
      System.out.println(automatWS.getAddress());
    }
  }

}