【问题标题】:How to post SOAP Request from .NET?如何从 .NET 发布 SOAP 请求?
【发布时间】:2008-11-13 14:49:00
【问题描述】:

我在 XML 文件中有 SOAP 请求。我想将请求发布到 .net 中的 Web 服务 如何实施?

【问题讨论】:

    标签: .net xml web-services soap


    【解决方案1】:
    var uri = new Uri("http://localhost/SOAP/SOAPSMS.asmx/add");
    
    var req = (HttpWebRequest) WebRequest.CreateDefault(uri); 
    req.ContentType = "text/xml; charset=utf-8"; 
    req.Method = "POST"; 
    req.Accept = "text/xml"; 
    req.Headers.Add("SOAPAction", "http://localhost/SOAP/SOAPSMS.asmx/add"); 
    
    var strSoapMessage = @"<?xml version='1.0' encoding='utf-8'?>
    <soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' 
                   xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
                   xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
      <soap:Body><add xmlns='http://tempuri.org/'><a>23</a><b>5</b></soap:Body>
    </soap:Envelope>"; 
    
    using (var stream = new StreamWriter(req.GetRequestStream(), Encoding.UTF8)) 
        stream.Write(strSoapMessage); 
    

    【讨论】:

    • 嗨,如果我的 xml 中没有 SOAP ACTION 值怎么办
    【解决方案2】:

    我做过这样的事情,手动构建一个 xml 请求,然后使用 webrequest 对象提交请求:

    string data = "the xml document to submit";
    string url = "the webservice url";
    string response = "the response from the server";
    
    // build request objects to pass the data/xml to the server
    byte[] buffer = Encoding.ASCII.GetBytes(data);
    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = buffer.Length;
    Stream post = request.GetRequestStream();
    
    // post data and close connection
    post.Write(buffer, 0, buffer.Length);
    post.Close();
    
    // build response object
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    Stream responsedata = response.GetResponseStream();
    StreamReader responsereader = new StreamReader(responsedata);
    response = responsereader.ReadToEnd();
    

    代码开头的字符串变量是您设置的,然后您会从服务器获得字符串响应(希望...)。

    【讨论】:

    • 现在我正在尝试您发布的相同代码。但我得到了不受支持的媒体类型
    • 这将是 request.ContentType,您应该尝试 'text/xml'(我认为)作为标准的 asp.net Web 服务。
    • 它已经为 ASMX 服务工作了。现在我想发布 WCF 服务的 SOAP 请求。如何实现它。
    • 使用此代码(在修复了名为“response”的两个变量的错误之后)并将内容类型更改为“text/xml”对我有用。
    【解决方案3】:

    这不是正常的方式。通常您会使用 WCF 或旧式 Web 服务引用来为您生成代理客户端。

    但是,您通常需要做的是使用 HttpWebRequest 连接到 URL,然后在请求正文中发送 XML。

    【讨论】:

      【解决方案4】:

      我想知道 XML 是如何生成的,它是有效的 SOAP 消息吗?您可以按照上述人员的建议通过 HTTP 发布它。

      如果你想测试这是否可行,你可以试试SoapUI(我的意思是测试)。

      【讨论】:

        【解决方案5】:

        这是另一个例子——这个在 VB 中:

            Dim manualWebClient As New System.Net.WebClient()
        
            manualWebClient.Headers.Add("Content-Type", "application/soap+xml;  charset=utf-8")
        
            ' Note: don't put the <?xml... tag in--otherwise it will blow up with a 500 internal error message!
            Dim bytArguments As Byte() = System.Text.Encoding.ASCII.GetBytes( _
                "<soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">" & System.Environment.NewLine & _
                "  <soap12:Body>" & System.Environment.NewLine & _
                "    <Multiply xmlns=""http://cptr446.class/"">" & System.Environment.NewLine & _
                "      <x>5</x>" & System.Environment.NewLine & _
                "      <y>4</y>" & System.Environment.NewLine & _
                "    </Multiply>" & System.Environment.NewLine & _
                "  </soap12:Body>" & System.Environment.NewLine & _
                "</soap12:Envelope>")
            Dim bytRetData As Byte() = manualWebClient.UploadData("http://localhost/CPTR446.asmx", "POST", bytArguments)
        
            MessageBox.Show(System.Text.Encoding.ASCII.GetString(bytRetData))
        

        【讨论】:

          【解决方案6】:

          很抱歉碰到了一个旧线程,这是我的解决方案

          ''' <summary>
          ''' Sends SOAP to a web service and sends back the XML it got back.
          ''' </summary>
          Public Class SoapDispenser
              Public Shared Function CallWebService(ByVal WebserviceURL As String, ByVal SOAP As String) As XmlDocument
                  Using wc As New WebClient()
                      Dim retXMLDoc As New XmlDocument()
          
                      wc.Headers.Add("Content-Type", "application/soap+xml; charset=utf-8")
                      retXMLDoc.LoadXml(wc.UploadString(WebserviceURL, SOAP))
          
                      Return retXMLDoc
                  End Using
              End Function
          End Class
          

          【讨论】:

            【解决方案7】:

            您需要通过 HTTP 发布数据。使用WebRequest class 发布数据。您将需要在发布请求中发送其他数据,以确保您拥有有效的 SOAP 信封。阅读SOAP spec了解所有详细信息。

            【讨论】:

            • 大声笑,“阅读肥皂规格”……多年后,他留着胡子回来,被送进了精神病院。
            • SOAP 规范相对简单,最糟糕的是 WS-whatever 规范。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-07-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多