You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用DHL searchLocations WebService遇权限错误求助

解决DHL searchLocations WebService "Access denied. Access key is not valid" 异常

你的问题核心很明确:身份验证失败导致访问被拒绝。咱们一步步拆解问题,把代码和配置都修正过来:

1. 先搞定合法的DHL API凭证

你代码里用的test/test是无效的测试账号,DHL的这个WebService需要真实的、经过官方授权的访问凭证。你需要:

  • 去DHL开发者平台申请对应的API用户名和密码
  • 确保你的凭证已经开通了searchLocations服务的访问权限

2. 修正身份验证的头部格式

你直接把Username和Password放到HTTP头部的方式不对,DHL这个服务用的是标准Basic Auth(基础身份验证),需要把用户名密码编码后放到Authorization头部里:

// 替换成你的真实凭证
String username = "你的DHL用户名";
String password = "你的DHL密码";

// 生成Basic Auth的编码值
String auth = username + ":" + password;
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
String authHeader = "Basic " + new String(encodedAuth);

// 设置到请求头部
Map<String, List<String>> headers = new HashMap<>();
headers.put("Authorization", Collections.singletonList(authHeader));
req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);

注意要导入java.util.Base64和java.nio.charset.StandardCharsets这两个类。

3. 修复Endpoint地址错误

你把服务调用地址设置成了WSDL的URL(带?wsdl后缀),这是错误的!WSDL是服务描述文件,实际调用的端点地址应该去掉后缀:

req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "https://standorte.deutschepost.de/webservice/");

4. 修正代码里的小细节

  • 街道名拼写错误:harles-de-Gaulle-Str应该改成Charles-de-Gaulle-Str(首字母大写)
  • getPackstationsByAddress的第一个参数是语言编码,别传空字符串,建议传"de"或者"en"

修复后的完整代码示例

import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Base64;
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("Charles-de-Gaulle-Str"); // 修正拼写错误
        request.setStreetNo("20");
        request.setZip("53113");
        List<AutomatWS> packstationsByAddress = new ArrayList<AutomatWS>();
        String wsdlLocation = "https://standorte.deutschepost.de/webservice/?wsdl";
        
        // 替换为你的真实DHL凭证
        String dhlUsername = "你的DHL用户名";
        String dhlPassword = "你的DHL密码";

        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();
            // 设置正确的Endpoint地址(去掉?wsdl)
            req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "https://standorte.deutschepost.de/webservice/");
            
            // 设置Basic Auth头部
            String auth = dhlUsername + ":" + dhlPassword;
            byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
            String authHeader = "Basic " + new String(encodedAuth);
            
            Map<String, List<String>> headers = new HashMap<>();
            headers.put("Authorization", Collections.singletonList(authHeader));
            req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
            
            // 传入正确的语言代码
            packstationsByAddress = webServiceImplPort.getPackstationsByAddress("de", request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        for (AutomatWS automatWS : packstationsByAddress) {
            System.out.println(automatWS.getAddress());
        }
    }
}

最后再确认几个点

  • 检查你的网络环境是否能正常访问DHL的服务(没有防火墙或代理拦截)
  • 确保你的凭证已经激活,没有权限限制
  • 如果还是有问题,建议对照DHL官方文档确认身份验证的具体要求

内容的提问来源于stack exchange,提问作者JavaMind

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:46:34