這是用於調用某個SOAP Web服務的XML,通過C#調用它可以將其作爲服務引用添加到C#項目中。
你需要你的服務的鏈接WSDL(Web服務定義語言文件),那麼你可以將服務引用添加到您的項目,並以這種方式很容易地調用它的任何功能:
1-定義客戶端用它來調用服務:
MyTestServiceSoapClient client = new MyTestServiceSoapClient();
2-調用此客戶端的一些方法是這樣的:
client.calculate_something("test_user", "test_password", "");
或者這樣:
client.calculate_something(new Input_data()
{ code_user = "test_user", password_broker = "test_password", subuser_id = ""}
);
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.calculate_something("test_user", "test_password", "");
string requestXML = requestInterceptor.LastRequestXML;
string responseXML = requestInterceptor.LastResponseXML;
// Now the xml you need is in "requestXML" variable