我正在尝试连接 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());
}
}
}
我弄清楚了问题所在,终点需要在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());
}
}
}