繁体   English   中英

我不知道如何使用 C# ASP.NET 进行 Soap 请求

[英]I don't know how to do a Soap Request using C# ASP.NET

我已经尝试了几个小时向我的 Soap 客户端 (SoapUI) 发出请求,阅读文档或回收代码(是的,抱歉),但我无法发出简单的请求。

我想做的很简单:

  • 用户插入 CustomerId 和邮件
  • CustomerId 转到我的 HomeController
  • HomeController 向 Soap 客户端发出请求
  • 用户通过邮件收到包含更多信息的 PDF 文件。

就这样。

一些代码:

 //Receive parameters from Html
 [HttpPost]
 public ActionResult ConexionWS(string customerId, string mail)

 //Connect to client with the authentication
 SoapClient client = new SoapClient(binding, wsdl);
 client.ClientCredentials.UserName.UserName = userN;
 client.ClientCredentials.UserName.Password = _pasw;
 client.Open();

 XmlDocument soapEnvelopeXml = GetSoapString(customerId);
 HttpWebRequest webRequest = CreateWebRequest(address, _action);
 InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

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

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

我发送 CustomerId 的 XML:

private static XmlDocument GetSoapString(string customerId){
  <urn:ZWsCustomerInvoiceGetList>
               <Customerid>"+customerId+"</Customerid>
               <Pedido1></Pedido1>                             
               <Pedido2></Pedido2>            
               <TFacturaMat>                    
                   <item>                            
                      <Food></Food>              
                      <Drink></Drink>              
                      <Souvenir></Souvenir>              
                   </item>                           
               </TFacturaMat>
</urn:ZWsCustomerInvoiceGetList>} 

我期望的 XML:

<urn:ZWsCustomerInvoiceGetList>
               <Customerid>"+customerId+"</Customerid>
               <Pedido1></Pedido1>                             
               <Pedido2></Pedido2>            
               <rests>                    
                   <item>                            
                      <Food>Meal</Food>              
                      <Drink>Water</Drink>              
                      <Souvenir>Nothing</Souvenir>             
                   </item>                           
               </rests>
</urn:ZWsCustomerInvoiceGetList>

我的问题:

  • 我不明白 CreateWebRequest 中的“action”参数中的内容
  • System.Xml.XmlException: ''http' 是一个意外的标记。 预期的标记是 '"' 或 '''。第 1 行,位置 33。' 在 GetSoapString 中
  • 我不知道如何使用 Visual Studio 在 Reference.cs 中为我提供的引用。

我很抱歉这么长的文字。 我无法弄清楚如何做到这一点。 是我的第一个 Web 服务,这对我来说似乎是不可能的。

SOAPAction 是端点 url。

像这样:

headers.addHeader("SOAPAction", endpointURL);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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