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

How to Send and Receive SOAP Requests and Responses in C#?

Susan Sarandon
Susan SarandonOriginal
2025-01-24 06:47:15264browse

How to Send and Receive SOAP Requests and Responses in C#?

Sending SOAP Requests and Receiving Responses in C

Problem

You need to create a C# client that sends SOAP requests to a web service and receives the responses.

Solution

Here's a code snippet that demonstrates how to achieve this:

using System.Net;
using System.IO;
using System.Xml;

public static void CallWebService()
{
    // Replace with your SOAP endpoint URL
    var url = "http://example.com/service.asmx";
    // Replace with your SOAP action
    var action = "http://example.com/service.asmx?op=HelloWorld";

    var soapEnvelope = CreateSoapEnvelope();
    var webRequest = CreateWebRequest(url, action);
    InsertSoapEnvelopeIntoWebRequest(soapEnvelope, webRequest);

    // Send the SOAP request
    var asyncResult = webRequest.BeginGetResponse(null, null);
    asyncResult.AsyncWaitHandle.WaitOne();

    // Receive the SOAP response
    var soapResponse = "";
    using (var webResponse = webRequest.EndGetResponse(asyncResult))
    {
        using (var reader = new StreamReader(webResponse.GetResponseStream()))
        {
            soapResponse = reader.ReadToEnd();
        }
    }
}

private static HttpWebRequest CreateWebRequest(string url, string action)
{
    var 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()
{
    var soapEnvelope = new XmlDocument();
    soapEnvelope.LoadXml(
        $@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/""
               xmlns:xsi=""http://www.w3.org/1999/XMLSchema-instance"" 
               xmlns:xsd=""http://www.w3.org/1999/XMLSchema"">
          <SOAP-ENV:Body>
            <HelloWorld xmlns=""http://tempuri.org/""
                SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/"">
                <int1 xsi:type=""xsd:integer"">12</int1>
                <int2 xsi:type=""xsd:integer"">32</int2>
            </HelloWorld>
          </SOAP-ENV:Body>
        </SOAP-ENV:Envelope>");
    return soapEnvelope;
}

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

This code creates a web request, sets the SOAP action header, and inserts the SOAP envelope into the request. It then sends the request and reads the SOAP response.

The above is the detailed content of How to Send and Receive SOAP Requests and Responses 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