I'm trying to invoke a SOAP webservice to validate VAT codes. The code works in Eclipse, but I'm getting below fault-string when invoking same code from JDeveloper 10.1.3 as that is where I need to integrate it with my rest of Oracle EBS code. I'm really stuck and looking for suggestions to get over the error.
<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.
Code snippet:
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();
}
}
Update: I could get it working using normal HTTP request following one of the answers on this site. Would still be interested to see if there is a better way of achieving this using javax.xml.soap package.
Working code using HTTP Request:
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();
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.