基于VIES服务创建SOAP请求检查归档中VAT有效性的技术问询
完整实现VIES VAT编号有效性检查的SOAP请求代码
我来帮你补全并优化这段调用VIES Web服务的代码,实现VAT编号的有效性检查。下面是完整的可运行实现,包含SOAP信封构建、请求发送和响应解析的全部逻辑:
import org.apache.axis.AxisFault; import javax.xml.rpc.Service; import javax.xml.rpc.ServiceException; import javax.xml.soap.*; import java.net.URL; public class test_vies { public static void main(String[] args) throws Exception { // VIES服务的SOAP端点 String endpoint = "http://ec.europa.eu/taxation_customs/vies/checkVatService"; // 正确的SOAP动作(必须匹配WSDL定义) String action = "http://ec.europa.eu/taxation_customs/vies/checkVat"; // 示例:检查德国的VAT编号DE123456789 checkVatNumber(endpoint, action, "DE", "123456789"); } private static void checkVatNumber(String endpoint, String action, String countryCode, String vatNumber) throws Exception { // 创建SOAP连接 SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection soapConnection = soapConnectionFactory.createConnection(); try { // 创建SOAP消息 MessageFactory messageFactory = MessageFactory.newInstance(); SOAPMessage soapMessage = messageFactory.createMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); // 定义命名空间 String namespaceURI = "http://ec.europa.eu/taxation_customs/vies"; SOAPEnvelope envelope = soapPart.getEnvelope(); envelope.addNamespaceDeclaration("vies", namespaceURI); // 创建SOAP Body和请求元素 SOAPBody soapBody = envelope.getBody(); SOAPElement checkVatElement = soapBody.addChildElement("checkVat", "vies"); SOAPElement countryCodeElement = checkVatElement.addChildElement("countryCode", "vies"); countryCodeElement.addTextNode(countryCode); SOAPElement vatNumberElement = checkVatElement.addChildElement("vatNumber", "vies"); vatNumberElement.addTextNode(vatNumber); // 设置SOAP动作头 MimeHeaders headers = soapMessage.getMimeHeaders(); headers.addHeader("SOAPAction", action); // 保存消息 soapMessage.saveChanges(); // 发送SOAP请求并获取响应 URL endpointUrl = new URL(endpoint); SOAPMessage soapResponse = soapConnection.call(soapMessage, endpointUrl); // 解析响应结果 parseSoapResponse(soapResponse); } finally { // 关闭连接 soapConnection.close(); } } private static void parseSoapResponse(SOAPMessage soapResponse) throws Exception { SOAPPart soapPart = soapResponse.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); SOAPBody soapBody = envelope.getBody(); // 获取响应元素 SOAPElement responseElement = (SOAPElement) soapBody.getChildElements(envelope.createName("checkVatResponse", "vies", "http://ec.europa.eu/taxation_customs/vies")).next(); // 提取各个返回字段 String valid = getElementValue(responseElement, "valid"); String name = getElementValue(responseElement, "name"); String address = getElementValue(responseElement, "address"); // 输出结果 System.out.println("VAT编号有效性检查结果:"); System.out.println("- 是否有效:" + valid); System.out.println("- 企业名称:" + (name.isEmpty() ? "未提供" : name)); System.out.println("- 企业地址:" + (address.isEmpty() ? "未提供" : address)); } private static String getElementValue(SOAPElement parentElement, String elementName) { try { SOAPElement element = (SOAPElement) parentElement.getChildElements(parentElement.createName(elementName)).next(); return element.getTextContent().trim(); } catch (Exception e) { return ""; } } }
关键部分说明
- SOAP动作配置:之前的空
action会导致请求失败,必须设置为WSDL定义的http://ec.europa.eu/taxation_customs/vies/checkVat - 请求体构造:严格按照VIES服务的要求,传入
countryCode(两位国家代码)和vatNumber(不带国家代码的编号部分) - 响应解析:从
checkVatResponse中提取核心结果,包括有效性状态、企业名称和地址 - 资源管理:使用
finally块确保SOAP连接被正确关闭,避免资源泄漏
注意事项
- 确保项目中引入了Apache Axis的相关依赖(如果用Maven,可添加Axis的依赖坐标)
- VIES服务有调用频率限制,短时间内大量请求会被临时封禁
- 处理可能的异常:比如
AxisFault(服务不可用、输入格式错误)、SOAPException(SOAP消息构造失败)等
内容的提问来源于stack exchange,提问作者Nicola Ferri
相关产品推荐
相关产品推荐

