您需要使用“添加服务引用”而不是“添加 Web 引用”功能来使用此解决方案,如果服务是 ASMX 或 WCF,则可以使用它。 (您需要使用 .NET Framework 3.X 才能使用此功能)
This article 将帮助您将服务引用添加到您的 C# 项目中。
要拦截请求和响应的XML,实现这两个类:
public class InspectorBehavior : IEndpointBehavior
{
public string LastRequestXML {
get
{
return myMessageInspector.LastRequestXML;
}
}
public string LastResponseXML {
get
{
return myMessageInspector.LastResponseXML;
}
}
private MyMessageInspector myMessageInspector = new MyMessageInspector();
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
public void Validate(ServiceEndpoint endpoint)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(myMessageInspector );
}
}
public class MyMessageInspector : IClientMessageInspector
{
public string LastRequestXML { get; private set; }
public string LastResponseXML { get; private set; }
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
LastResponseXML = reply.ToString();
}
public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
{
LastRequestXML = request.ToString();
return request;
}
}
然后,将调用代码更改为:
MyTestServiceSoapClient client = new MyTestServiceSoapClient();
var requestInterceptor = new InspectorBehavior();
client.Endpoint.Behaviors.Add(requestInterceptor );
client.DoSomething("param1", "param2", "param3");
string requestXML = requestInterceptor.LastRequestXML;
string responseXML = requestInterceptor.LastResponseXML;
****编辑****
这与服务器端技术无关,您可以将它与 WCF、ASMX、PHP、... Web 服务一起使用,我已经测试过:
http://www.w3schools.com/webservices/tempconvert.asmx
并得到以下 XML:
requestXML=
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/CelsiusToFahrenheit</Action>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CelsiusToFahrenheit xmlns="http://tempuri.org/">
<Celsius>50</Celsius>
</CelsiusToFahrenheit>
</s:Body>
</s:Envelope>
responseXML=
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<s:Header xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" />
<soap:Body>
<CelsiusToFahrenheitResponse xmlns="http://tempuri.org/">
<CelsiusToFahrenheitResult>122</CelsiusToFahrenheitResult>
</CelsiusToFahrenheitResponse>
</soap:Body>
</soap:Envelope>
****编辑 2****
“添加Web引用”不是ASMX专用的,不是ASMX客户端技术,“添加服务引用”不是WCF客户端技术,可以同时使用ASMX、WCF、 JSP 开发或 PHP 开发的 web 服务,您需要您的应用程序使用 .Net framework 3.5 才能使用“添加服务引用”。
This article 提及:
在 Visual Studio 中使用“添加 Web 引用”对话框时,客户端
代理使用 WSDL 信息生成并添加到 Visual
工作室项目。这通常用于 ASMX 服务,但您可以
还可以使用“添加 Web 引用”对话框为
WCF 服务。但是,您需要手动键入服务 URL,并且
生成的代理使用 XML 序列化,这是唯一的
支持的序列化类型。为 WCF 创建客户端代理
支持数据协定序列化程序的服务,您可以使用
Svcutil.exe 工具或使用添加服务引用功能的
.NET Framework 3.x 的 Visual Studio 开发工具。