簡體   English   中英

從C#直接將原始SOAP XML發送到WCF服務

[英]Sending raw SOAP XML directly to WCF service from C#

我有一個WCF服務參考:

http://.../Service.svc(?WSDL)

我有一個包含兼容SOAP信封的XML文件

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <MyXML>
       ...

現在,我想通過一些C#代碼將這些原始數據直接發送到服務(並接收響應),而不使用Visual Studio服務引用。

這是可能的,如果是的話,怎么樣?

你可以使用UploadString 您需要適當地設置Content-TypeSOAPAction標頭:

class Program
{
    static void Main(string[] args)
    {
        using (var client = new WebClient())
        {
            // read the raw SOAP request message from a file
            var data = File.ReadAllText("request.xml");
            // the Content-Type needs to be set to XML
            client.Headers.Add("Content-Type", "text/xml;charset=utf-8");
            // The SOAPAction header indicates which method you would like to invoke
            // and could be seen in the WSDL: <soap:operation soapAction="..." /> element
            client.Headers.Add("SOAPAction", "\"http://www.example.com/services/ISomeOperationContract/GetContract\"");
            var response = client.UploadString("http://example.com/service.svc", data);
            Console.WriteLine(response);
        }
    }
}

我只想評論Darin的回復對我有用,除了我必須取出SOAPAction頭值附加的額外引號(當然替換你的uri):

client.Headers.Add("SOAPAction", "http://www.example.com/services/ISomeOperationContract/GetContract");

您可以嘗試使用webclient類並將xml發布到服務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM