简体   繁体   中英

C# issue calling EORI SOAP Service

I am trying to call the EORI validation open interface from within a C# application but am not getting anywhere. I've looked around the site and there doesn't seem to be any documentation on how to do this.

Site: http://ec.europa.eu/taxation_customs/dds2/eos/news/newstar.jsp?Lang=en

WSDL: http://ec.europa.eu/taxation_customs/dds2/eos/validation/services/validation?wsdl

I've created a new C# Console App and added the WSDL as a service reference then tried calling the service but get the following exception...

System.ServiceModel.CommunicationException: 'The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error.'

I've used the online tool with the number and it returns data as expected http://ec.europa.eu/taxation_customs/dds2/eos/eori_validation.jsp?Lang=en

Has anybody else had any luck with this?

Thanks

If you Google the URI that is deep in the Reference.cs file that opens if you open the definition of the EORI validation methods, then you'll that someone on this page here https://www.codeproject.com/Questions/1075553/Soap-Message-Format-Issue-while-accessing-webservi is having the same issue.

On that page he quotes this sample code that he is using to make a query.

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
 <soap:Body> 
<ev:validateEORI xmlns:ev="http://eori.ws.eos.dds.s/"> 
<ev:eori>DE123456</ev:eori> 
<ev:eori>IT123456789</ev:eori> 
</ev:validateEORI> 
</soap:Body> 
</soap:Envelope> 

Try this code in Postman, and enjoy the results. :D

His query is ultimately that the C# code he wrote isn't creating valid XML, but at least this XML will get you results from the API for your testing / development process to proceed.

Thanks for the help, In case anyone else was struggling, below is the helper class for making and sending the request.

public class EoriModel
    {
        string _url;

        public EoriModel()
        {
            _url = "http://ec.europa.eu/taxation_customs/dds2/eos/validation/services/validation";  
        }

        public EoriResponseModel ValidateEoriNumber(string number)
        {
            if (number == null)
            {
                return null;
            }

            XmlDocument soapEnvelopeXml = CreateSoapEnvelope(number);
            HttpWebRequest webRequest = CreateWebRequest(_url);
            InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

            IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
            asyncResult.AsyncWaitHandle.WaitOne();

            string response;
            using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
            {
                using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                {
                    response = rd.ReadToEnd();
                }
            }

            int startPos = response.IndexOf("<return>");
            int lastPos = response.LastIndexOf("</return>") - startPos + 9;
            string responseFormatted = response.Substring(startPos, lastPos);

            XmlSerializer serializer = new XmlSerializer(typeof(EoriResponseModel));
            EoriResponseModel result; 

            using (TextReader reader = new StringReader(responseFormatted))
            {
                result = (EoriResponseModel)serializer.Deserialize(reader);
            }

            return result;
        }

        private static HttpWebRequest CreateWebRequest(string url)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            return webRequest;
        }

        private static XmlDocument CreateSoapEnvelope(string number)
        {
            XmlDocument soapEnvelopeDocument = new XmlDocument();
            StringBuilder xmlBuilder = new StringBuilder();
            xmlBuilder.AppendFormat("<soap:Envelope xmlns:soap={0} >", "'http://schemas.xmlsoap.org/soap/envelope/'");
            xmlBuilder.Append("<soap:Body>");
            xmlBuilder.AppendFormat("<ev:validateEORI xmlns:ev={0} >", "'http://eori.ws.eos.dds.s/'");
            xmlBuilder.AppendFormat("<ev:eori>{0}</ev:eori>", number);
            xmlBuilder.Append("</ev:validateEORI>");
            xmlBuilder.Append("</soap:Body> ");
            xmlBuilder.Append("</soap:Envelope> ");

            soapEnvelopeDocument.LoadXml(xmlBuilder.ToString());
            return soapEnvelopeDocument;
        }

        private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
        {
            using (Stream stream = webRequest.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }
        }
    }

And the class with annotation used to parse the results

[XmlRoot(ElementName = "return")]
    public class EoriResponseModel
    {
        [XmlElement(ElementName = "requestDate")]
        public string RequestDate { get; set; }
        [XmlElement(ElementName = "result")]
        public List<Result> Result { get; set; }
    }

    [XmlRoot(ElementName = "result")]
    public class Result
    {
        [XmlElement(ElementName = "eori")]
        public string Eori { get; set; }
        [XmlElement(ElementName = "status")]
        public string Status { get; set; }
        [XmlElement(ElementName = "statusDescr")]
        public string StatusDescr { get; set; }
        [XmlElement(ElementName = "name")]
        public string Name { get; set; }
        [XmlElement(ElementName = "street")]
        public string Street { get; set; }
        [XmlElement(ElementName = "postalCode")]
        public string PostalCode { get; set; }
        [XmlElement(ElementName = "city")]
        public string City { get; set; }
        [XmlElement(ElementName = "country")]
        public string Country { get; set; }
    }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM