调用EORI SOAP服务的C#问题

时间:2019-02-06 10:26:32

标签: c# api web soap

我正在尝试从C#应用程序中调用EORI验证打开接口,但没有到达任何地方。 我环顾了整个站点,似乎没有任何有关此操作的文档。

站点: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

我创建了一个新的C#控制台应用程序,并将WSDL添加为服务引用,然后尝试调用该服务,但得到以下异常...

  

System.ServiceModel.CommunicationException:'服务器没有   提供有意义的答复;这可能是由于合同引起的   不匹配,会话过早关闭或内部服务器错误。'

我已经使用了带有数字的在线工具,它可以按预期返回数据 http://ec.europa.eu/taxation_customs/dds2/eos/eori_validation.jsp?Lang=en

其他人对此有好运吗?

谢谢

2 个答案:

答案 0 :(得分:0)

如果您打开打开EORI验证方法的定义后打开的Reference.cs文件中包含的URI,那么您会发现此页面https://www.codeproject.com/Questions/1075553/Soap-Message-Format-Issue-while-accessing-webservi上的某人也遇到了同样的问题

在该页面上,他引用了他用于进行查询的示例代码。

<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> 

在Postman中尝试此代码,并享受结果。 :D

他的查询最终是他编写的C#代码没有创建有效的XML,但是至少此XML将从API中获得结果,以便您进行测试/开发过程。

答案 1 :(得分:0)

感谢您的帮助,以防万一其他人在挣扎,下面是用于发出和发送请求的帮助程序类。

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);
            }
        }
    }

带有注释的类用于解析结果

[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; }
    }