【问题标题】:SOAP Response Error reading XMLStreamReader读取 XMLStreamReader 的 SOAP 响应错误
【发布时间】:2020-12-24 18:56:31
【问题描述】:

我正在尝试调用 SOAP 网络服务来验证增值税代码。该代码在 Eclipse 中工作,但是当我从 JDeveloper 10.1.3 调用相同的代码时,我遇到了故障字符串,因为我需要将它与我的其余 Oracle EBS 代码集成。我真的很困惑,正在寻找解决错误的建议。

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>Error reading XMLStreamReader.</faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
Process exited with exit code 0.

代码sn-p:

package xxtest.vatvalidation.;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

import org.w3c.dom.NodeList;

public class VATValidationTest {
    
            public static void main(String[] args) throws Exception {
        String url = "http://ec.europa.eu/taxation_customs/vies/services/checkVatService";
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();
        
          URL endpoint = new URL(null, url, new URLStreamHandler() { 
              protected URLConnection openConnection(URL url) throws IOException { 
                  URL clone = new URL(url.toString()); 
                  URLConnection connection = null; 
                  Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(<proxy address>, <proxy port>)); 
                  connection = clone.openConnection(proxy); 
                  return connection; } });

        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();
        SOAPEnvelope envelope = soapPart.getEnvelope();
        
        String serverURI = "urn:ec.europa.eu:taxud:vies:services:checkVat:types";
        envelope.addNamespaceDeclaration("urn", serverURI);
        
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyElem = soapBody.addChildElement("checkVat", "urn");
        soapBodyElem.addChildElement("countryCode", "urn").addTextNode("NL");
        SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("vatNumber", "urn");
        soapBodyElem2.addTextNode("NL999999999");

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", serverURI  + "#checkVat");
        
        soapMessage.saveChanges();
        soapMessage.writeTo(System.out);
        SOAPMessage soapResponse = soapConnection.call(soapMessage,  endpoint);
        //NodeList nList = soapResponse.getSOAPBody().getElementsByTagName("valid");
        soapResponse.writeTo(System.out);
        soapConnection.close();
    }

}

【问题讨论】:

    标签: java web-services soap


    【解决方案1】:

    更新:我可以按照本网站上的答案之一使用正常的 HTTP 请求使其工作。仍然有兴趣看看是否有更好的方法来使用 javax.xml.soap 包实现这一点。

    使用 HTTP 请求的工作代码:

    package xxtest.vatvalidation;
    
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    
    import java.io.InputStream;
    
    import java.io.InputStreamReader;
    
    import java.net.HttpURLConnection;
    import java.net.InetSocketAddress;
    import java.net.Proxy;
    import java.net.URL;
    
    public class VATValidationHttp {
        public static void main(String[] args) {
            String targetURL = "http://ec.europa.eu/taxation_customs/vies/services/checkVatService";
            String urlParameters = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:ec.europa.eu:taxud:vies:services:checkVat:types\">"
                      +"<soapenv:Body>"
                         +"<urn:checkVat>"
                           +"<urn:countryCode>IE</urn:countryCode>"
                            +"<urn:vatNumber>IE9999999</urn:vatNumber>"
                         +"</urn:checkVat>"
                      +"</soapenv:Body>"
                   +"</soapenv:Envelope>";
            HttpURLConnection connection = null;
    
            try {
                //Create connection
                URL url = new URL(targetURL);
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(<MY_PROXY_HOST>, <MY_PROXY_PORT>)); 
                connection = (HttpURLConnection)url.openConnection(proxy);
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Type", 
                                              "text/xml;charset=UTF-8");
    
                connection.setRequestProperty("Content-Length", 
                                              Integer.toString(urlParameters.getBytes().length));
                connection.setRequestProperty("Content-Language", "en-US");
                connection.setRequestProperty("Host", "ec.europa.eu");
                connection.setRequestProperty("Connection", "Keep-Alive");
    
                connection.setUseCaches(false);
                connection.setDoOutput(true);
    
                //Send request
                DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
                wr.writeBytes(urlParameters);
                wr.close();
    
                //Get Response  
                InputStream is = connection.getInputStream();
                BufferedReader rd = new BufferedReader(new InputStreamReader(is));
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = rd.readLine()) != null) {
                    response.append(line);
                    response.append('\r');
                }
                rd.close();
                System.out.println( response.toString());
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println( e.getMessage());
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多