Home >Backend Development >C++ >How to Create a C# Windows Service to Send SOAP Requests and Receive Responses?

How to Create a C# Windows Service to Send SOAP Requests and Receive Responses?

Susan Sarandon
Susan SarandonOriginal
2025-01-24 06:41:08640browse

How to Create a C# Windows Service to Send SOAP Requests and Receive Responses?

C# Windows Service: Send SOAP request and receive response

This article describes how to create a C# Windows service to send SOAP requests and receive responses. This method utilizes the native functions of C#, which is efficient and convenient.

The following code provides an alternative:

<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);

    // 开始异步调用Web请求
    IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

    // 暂停线程,直到调用完成
    asyncResult.AsyncWaitHandle.WaitOne();

    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(@"<envelope http:="" xmlns:soap-env="" xmlns:xsd="" xmlns:xsi=""><body><helloworld http:="" soap-env:encoding xmlns=""><int1 xsd:integer="" xsi:type=""></int1></helloworld></body>
    </envelope>");
    return soapEnvelopeDocument;
}

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

This method provides a flexible and efficient way to send SOAP requests and receive responses without relying on external libraries.

The above is the detailed content of How to Create a C# Windows Service to Send SOAP Requests and Receive Responses?. 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