这种错误通常是由于在服务 URI 中缺少协议前缀(如 http:// 或 https://)导致的。要解决此问题,只需要确保在调用服务时正确地添加协议前缀即可。
代码示例:
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection soapConnection = soapConnectionFactory.createConnection(); String url = "http://example.com/MyService"; // 错误的 URI MessageFactory messageFactory = MessageFactory.newInstance(); SOAPMessage soapMessage = messageFactory.createMessage(); SOAPBody soapBody = soapMessage.getSOAPBody(); SOAPElement soapElement = soapBody.addChildElement("MyRequest"); soapElement.addChildElement("Name").addTextNode("John"); soapElement.addChildElement("Age").addTextNode("30"); URL endpoint = new URL(url); SOAPMessage soapResponse = soapConnection.call(soapMessage, endpoint); // 这里会抛出异常 // 处理响应 soapConnection.close();