Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调用EORI SOAP服务的问题_C#_Api_Web_Soap - Fatal编程技术网

C# 调用EORI SOAP服务的问题

C# 调用EORI SOAP服务的问题,c#,api,web,soap,C#,Api,Web,Soap,我试图从C#应用程序中调用EORI验证开放接口,但没有任何进展。 我环顾了一下网站,似乎没有任何关于如何做到这一点的文档 地点: WSDL: 我创建了一个新的C#控制台应用程序,并将WSDL添加为服务引用,然后尝试调用该服务,但出现以下异常 System.ServiceModel.CommunicationException:'服务器未启动 提供有意义的答复;这可能是由合同引起的 不匹配、会话过早关闭或内部服务器错误。” 我使用了带有数字的在线工具,它按预期返回数据 还有谁在这件事上运气好吗

我试图从C#应用程序中调用EORI验证开放接口,但没有任何进展。 我环顾了一下网站,似乎没有任何关于如何做到这一点的文档

地点:

WSDL:

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

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

我使用了带有数字的在线工具,它按预期返回数据

还有谁在这件事上运气好吗


谢谢

如果你用谷歌搜索Reference.cs文件深处的URI,如果你打开EORI验证方法的定义,你会发现这个页面上有人有同样的问题

在那个页面上,他引用了他用来进行查询的示例代码

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

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


他的疑问最终是他编写的C#代码没有创建有效的XML,但至少该XML会从API中获取结果,以便继续测试/开发过程。

如果您搜索Reference.cs文件深处的URI,打开EORI验证方法的定义,该文件就会打开,然后你会发现这个页面上有人有同样的问题

在那个页面上,他引用了他用来进行查询的示例代码

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

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


他的疑问最终是他编写的C#代码没有创建有效的XML,但至少该XML会从API中获取结果,以便继续测试/开发过程。

感谢您的帮助,如果其他人遇到困难,下面是发出请求的帮助器类

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);
            }
        }
    }
公共类EoriModel
{
字符串url;
公共EoriModel()
{
_url=”http://ec.europa.eu/taxation_customs/dds2/eos/validation/services/validation";  
}
公共EoriResponseModel ValidateEoriNumber(字符串编号)
{
if(number==null)
{
返回null;
}
XmlDocument soapEnvelopeXml=CreateSOAPEvelope(编号);
HttpWebRequest webRequest=CreateWebRequest(_url);
插入SOAPEnvelopeInToWebRequest(SOAPEnvelopeExML,webRequest);
IAsyncResult asyncResult=webRequest.BeginGetResponse(null,null);
asyncResult.AsyncWaitHandle.WaitOne();
字符串响应;
使用(WebResponse WebResponse=webRequest.EndGetResponse(asyncResult))
{
使用(StreamReader rd=newstreamreader(webResponse.GetResponseStream()))
{
响应=rd.ReadToEnd();
}
}
int startPos=response.IndexOf(“”);
int lastPos=response.LastIndexOf(“”)-startPos+9;
string responseFormatted=response.Substring(startPos,lastPos);
XmlSerializer serializer=新的XmlSerializer(typeof(EoriResponseModel));
EoriResponseModel结果;
使用(TextReader=新的StringReader(响应格式))
{
结果=(EoriResponseModel)序列化程序。反序列化(读取器);
}
返回结果;
}
私有静态HttpWebRequest CreateWebRequest(字符串url)
{
HttpWebRequest webRequest=(HttpWebRequest)webRequest.Create(url);
webRequest.ContentType=“text/xml;charset=\”utf-8\”;
webRequest.Accept=“text/xml”;
webRequest.Method=“POST”;
返回webRequest;
}
私有静态XmlDocument CreateSoapEnvelope(字符串编号)
{
XmlDocument SOAPEnvelopedDocument=新的XmlDocument();
StringBuilder xmlBuilder=新的StringBuilder();
AppendFormat(“,”)http://schemas.xmlsoap.org/soap/envelope/'");
xmlBuilder.Append(“”);
AppendFormat(“,”)http://eori.ws.eos.dds.s/'");
AppendFormat(“{0}”,数字);
xmlBuilder.Append(“”);
xmlBuilder.Append(“”);
xmlBuilder.Append(“”);
LoadXml(xmlBuilder.ToString());
返回SOAPEnveloped文档;
}
私有静态void InsertSoapEnvelopeIntoWebRequest(XmlDocument SoapEnvelopeExML,HttpWebRequest webRequest)
{
使用(Stream-Stream=webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(流);
}
}
}
以及用于解析结果的带有注释的类

[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; }
    }
[XmlRoot(ElementName=“return”)]
公共类EoriResponseModel
{
[xmlement(ElementName=“requestDate”)]
公共字符串RequestDate{get;set;}
[xmlement(ElementName=“result”)]
公共列表结果{get;set;}
}
[XmlRoot(ElementName=“result”)]
公开课成绩
{
[XmlElement(ElementName=“eori”)]
公共字符串Eori{get;set;}
[xmlement(ElementName=“status”)]
公共字符串状态{get;set;}
[xmlement(ElementName=“statusDescr”)]
公共字符串StatusDescr{get;set;}
[xmlement(ElementName=“name”)]
公共字符串名称{get;set;}
[xmlement(ElementName=“street”)]
公共字符串Street{get;set;}
[XmlElement(ElementName=“postalCode”)]
公共字符串PostalCode{get;set;}
[xmlement(ElementName=“city”)]
公共字符串City{get;set;}
[xml元素(El)