【问题标题】:How can I call a SOAP Web Service without Adding Web Reference如何在不添加 Web 引用的情况下调用 SOAP Web 服务
【发布时间】:2014-12-23 13:06:18
【问题描述】:

我需要为基于 SOAP 的 Web 服务开发一个 .NET 4.5 客户端。问题是开发这些基于 SOAP 的服务的公司不提供 WSDL。但是,它们确实提供了请求响应模式(XSD 文件)。由于没有 WSDL,我无法添加 Web 引用并自动生成客户端代理代码。

是否有任何 .NET 4.5 库可供我用来进行这些 SOAP 基础服务调用?它还需要支持 SOAP 1.1 和 SOAP 附件。

【问题讨论】:

  • 一种选择是使用 WCF 并使用“通道层”。您仍然需要要使用的 WebService 的接口。
  • ...或者只是自己创建 WSDL 并使用它来“添加网络引用”。
  • 在您的情况下创建 WSDL 将是一个好主意,因为它将节省您处理可序列化实体的时间。如果他们配置了消息级别的安全性,那么这种方法对于服务也是安全的。

标签: c# .net wcf soap soap-client


【解决方案1】:

如果由于某种原因您不想创建 WSDL 文件,可以使用以下示例手动构造 SOAP HTTP 请求:

var url = Settings.Default.URL; //'Web service URL'
var action = Settings.Default.SOAPAction; //the SOAP method/action name

var soapEnvelopeXml = CreateSoapEnvelope();
var soapRequest = CreateSoapRequest(url, action);
InsertSoapEnvelopeIntoSoapRequest(soapEnvelopeXml, soapRequest);

using (var stringWriter = new StringWriter())
{
    using (var xmlWriter = XmlWriter.Create(stringWriter))
    {
        soapEnvelopeXml.WriteTo(xmlWriter);
        xmlWriter.Flush();
    }
}

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

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

if (!success) return null;

// get the response from the completed web request.
using (var webResponse = soapRequest.EndGetResponse(asyncResult))
{
    string soapResult;
    var responseStream = webResponse.GetResponseStream();
    if (responseStream == null)
    {
        return null;
    }
    using (var reader = new StreamReader(responseStream))
    {
        soapResult = reader.ReadToEnd();
    }
    return soapResult;
}

private static HttpWebRequest CreateSoapRequest(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(Settings.Default.SOAPEnvelope); //the SOAP envelope to send
    return soapEnvelope;
}

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

【讨论】:

  • 非常感谢马特。我想我会以此为基础,看看如何将 SOAP 附件也合并到请求中。非常感谢。
  • 我错了。hear.可以帮帮我吗?
  • 我为此摇头多年,收到 500 内部服务器错误 (Server did not recognize the value of HTTP Header SOAPAction: MyWebMethod) - 我终于发现您需要添加服务指定的命名空间,例如http://tempuri.org/MyWebMethod 作为action 参数!
  • 我在我的项目中使用这个示例,但我无法获得 HTTP 状态代码。我如何从中获取 HTTP 状态代码?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-11
  • 2011-06-25
  • 2011-01-12
  • 1970-01-01
  • 2014-09-11
  • 1970-01-01
相关资源
最近更新 更多