【问题标题】:SOAP object over HTTP post in C# .NETC# .NET 中通过 HTTP 发布的 SOAP 对象
【发布时间】:2009-11-25 18:27:30
【问题描述】:

我正在尝试在 C# .NET 中编写 SOAP 消息(包括标头)以使用 HTTP 发布发送到 URL。我想将它发送到的 URL 不是 Web 服务,它只是接收 SOAP 消息以最终从中提取信息。关于如何做到这一点的任何想法?

【问题讨论】:

    标签: c# .net soap http-post envelope


    【解决方案1】:

    首先您需要创建一个有效的 XML。我使用 Linq to XML 来实现这一点,如下所示:

    XNamespace soapenv = "http://schemas.xmlsoap.org/soap/envelope/";
    var document = new XDocument(
                   new XDeclaration("1.0", "utf-8", String.Empty),
                   new XElement(soapenv + "Envelope",
                       new XAttribute(XNamespace.Xmlns + "soapenv", soapenv),
                       new XElement(soapenv + "Header",
                           new XElement(soapenv + "AnyOptionalHeader",
                               new XAttribute("AnyOptionalAttribute", "false"),
                           )
                       ),
                       new XElement(soapenv + "Body",
                           new XElement(soapenv + "MyMethodName",
                                new XAttribute("AnyAttributeOrElement", "Whatever")
                           )
                       )
                    );
    

    然后我使用(EDIT:在此处添加XDocument.ToString()。)

                var req = WebRequest.Create(uri);
                req.Timeout = 300000;  //timeout
                req.Method = "POST";
                req.ContentType = "text/xml;charset=UTF-8";
    
                using (var writer = new StreamWriter(req.GetRequestStream()))
                {
                    writer.WriteLine(document.ToString());
                    writer.Close();
                }
    

    如果我必须阅读一些回复,我会阅读(这是上述代码的后续):

                using (var rsp = req.GetResponse())
                {
                    req.GetRequestStream().Close();
                    if (rsp != null)
                    {
                        using (var answerReader = 
                                    new StreamReader(rsp.GetResponseStream()))
                        {
                            var readString = answerReader.ReadToEnd();
                            //do whatever you want with it
                        }
                    }
                }
    

    【讨论】:

    • 你是对的,有一种更简单的方法可以将XDocument 转换为字符串。这是ToString() 方法。有一个重载可让您指定是否要对 XML 进行缩进和格式化(默认为格式化)。
    • 它允许你定义一个对象的生命周期,从而在它离开块时自动调用它的 dispose 方法。
    【解决方案2】:

    您上面的代码缺少括号并且有一个额外的逗号,我在这里修复了它:

    XNamespace soapenv = "http://schemas.xmlsoap.org/soap/envelope/"; 
    var document = new XDocument( 
        new XDeclaration("1.0", "utf-8", String.Empty), 
        new XElement(soapenv + "Envelope", 
            new XAttribute(XNamespace.Xmlns + "soapenv", soapenv), 
            new XElement(soapenv + "Header", 
                new XElement(soapenv + "AnyOptionalHeader", 
                    new XAttribute("AnyOptionalAttribute", "false")
                ) 
            ), 
            new XElement(soapenv + "Body", 
                new XElement(soapenv + "MyMethodName", 
                    new XAttribute("AnyAttributeOrElement", "Whatever") 
                ) 
            ) 
        )
    ); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-03
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      相关资源
      最近更新 更多