客户端发送SOAP请求并接收响应

数据库

尝试创建C#客户端(将作为Windows服务开发),该客户端将SOAP请求发送到Web服务(并获取结果)。

从这个问题中我看到了以下代码:

protected virtual WebRequest CreateRequest(ISoapMessage soapMessage)
{
    var 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; }
}

看起来不错,任何人都知道如何使用它,并且这是否是最佳实践?

KBB写

我通常使用另一种方法来做同样的事情

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

public static void CallWebService()
{
    var _url = "http://xxxxxxxxx/Service1.asmx";
    var _action = "http://xxxxxxxx/Service1.asmx?op=HelloWorld";

    XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
    HttpWebRequest webRequest = CreateWebRequest(_url, _action);
    InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

    // begin async call to web request.
    IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

    // suspend this thread until call is complete. You might want to
    // do something usefull here like update your UI.
    asyncResult.AsyncWaitHandle.WaitOne();

    // get the response from the completed web request.
    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(
    @"<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 soapEnvelopeDocument;
}

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

接收服务器对客户端请求的响应

如何通过Nuxt.js服务器从Nuxt.js客户端发送请求,以及如何将响应接收回客户端

如何接收客户端发送的数据

无法将json从客户端发送到php,并且无法从php接收json响应

Java套接字接收文件,然后将响应发送回客户端

如何发送请求并继续响应,然后发送回客户端?

Twisted中的异步客户端不发送/接收请求(使用NetStringReceiver)

如何在客户端使用cxf记录Soap请求的响应时间?

在客户端使用JAX-WS跟踪SOAP请求/响应

cxf ws 客户端向服务器发送请求的最佳实践(soap)

Web 服务器能否在客户端发送完整请求之前开始响应?

NodeJS/ExpressJS - 将 http 请求的响应发送回客户端

向客户端发送HTTP响应,而无需始终发出请求

IRC 客户端不打印/接收完整响应

Android Socket客户端无法发送和接收消息

Rust, libzmq, 客户端只发送消息,然后是接收

自动生成的 Soap 客户端不解析响应

RabbitMQ:客户端启动之前,客户端不接收发送的消息

(Socket.SendTo)如何响应/将数据发送回服务器开始接收的客户端?C#

如何使用FEIGN客户端发送SOAP对象?

如何使用HTML发送SOAP请求并接收响应?

PHP Soap 客户端请求时间测量

查看来自Java客户端的SOAP请求

具有客户端认证连接的HTTP客户端的SOAP请求超时异常

用Soap请求发送客户端证书和授权标头的理想方式是什么

根据客户端发送HTML或JSON响应

使用 FLASK 向所有客户端发送响应

向多个客户端发送HTTP响应

请求文件上传后在客户端掌握响应