Home >Backend Development >C++ >How to Send and Receive SOAP Requests in C#?

How to Send and Receive SOAP Requests in C#?

Linda Hamilton
Linda HamiltonOriginal
2025-01-24 06:36:10603browse

How to Send and Receive SOAP Requests in C#?

Send and receive SOAP requests using C#

This article describes how to create a C# client (developed as a Windows service) that sends a SOAP request to a web service and waits for the corresponding response.

Detailed code explanation

The following code example shows how to create a SOAP request and its corresponding response:

<code class="language-csharp">using System.Xml;
using System.Net;
using System.IO;

public static void CallWebService()
{
    string url = "http://xxxxxxxxx/Service1.asmx";
    string action = "http://xxxxxxxx/Service1.asmx?op=HelloWorld";

    XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
    HttpWebRequest webRequest = CreateWebRequest(url, action);
    InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

    webRequest.BeginGetResponse(null, null).AsyncWaitHandle.WaitOne(); // 开始异步调用 Web 请求。

    string soapResult;
    using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
    {
        using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
        {
            soapResult = rd.ReadToEnd();
        }
        Console.Write(soapResult);
    }
}

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 XmlDocument CreateSoapEnvelope()
{
    XmlDocument soapEnvelopeDocument = new XmlDocument();
    soapEnvelopeDocument.LoadXml(
    //... (XML 定义已省略)
    return soapEnvelopeDocument;
}

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

In this example:

  • CreateSoapEnvelope() Creates the requested SOAP envelope.
  • CreateWebRequest() Set up web requests with appropriate headers and configuration.
  • InsertSoapEnvelopeIntoWebRequest() Insert a SOAP envelope into the request.
  • BeginGetResponse() and EndGetResponse() are used for asynchronous SOAP request handling.

Another implementation suggestion

Another common approach involves using the WebRequest and WebResponse classes:

<code class="language-csharp">protected virtual WebRequest CreateRequest(ISoapMessage soapMessage)
{
    WebRequest wr = WebRequest.Create(soapMessage.Uri);
    wr.ContentType = "text/xml;charset=utf-8";
    wr.ContentLength = soapMessage.ContentXml.Length;

    wr.Headers.Add("SOAPAction", soapMessage.SoapAction);
    wr.Credentials = soapMessage.Credentials;
    wr.Method = "POST";
    wr.GetRequestStream().Write(Encoding.UTF8.GetBytes(soapMessage.ContentXml), 0, soapMessage.ContentXml.Length);

    return wr;
}

public interface ISoapMessage
{
    string Uri { get; }
    string ContentXml { get; }
    string SoapAction { get; }
    ICredentials Credentials { get; }
}</code>

This implementation provides a more direct way to handle SOAP requests.

The above is the detailed content of How to Send and Receive SOAP Requests in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn