代码之家  ›  专栏  ›  技术社区  ›  Tad Donaghe

如何查看对C中服务引用的传入响应的完整SOAP响应(包括头)?

  •  6
  • Tad Donaghe  · 技术社区  · 15 年前

    我有一个简单的C 3.5.NET控制台应用程序,它连接到一个服务引用。一切都很好——打电话和接收响应,但现在我被告知要查看返回消息中的SOAP头。

    我发现.NET WebService Studio非常棒,它将显示SOAP请求和SOAP响应。

    对于响应,它显示如下内容:

    ResponseCode: 200 (OK)
    Content-Language:en-US
    Content-Length:30048
    Content-Type:text/xml; charset=utf-8
    Date:Mon, 25 Jan 2010 19:57:47 GMT
    Server:WebSphere Application Server/6.1
    
    <?xml version="1.0" encoding="utf-16"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <soapenv:Header />
      <soapenv:Body>
    

    如何在应用程序中生成类似的内容?

    我感兴趣的响应是寻找一个不同的方法,它返回足够大的消息来炸毁WebServiceStudio。我不知道如何使用这个工具设置消息大小参数。所以,我只想自己捕捉这些信息。

    我能怎么做吗?

    2 回复  |  直到 13 年前
        1
  •  7
  •   Mikael Svenson    15 年前

    WCF通过 config file 或者,您也可以实现自己记录消息的行为。

    添加如下行为:

    Service1SoapClient client = new Service1SoapClient();
    client.Endpoint.Behaviors.Add( new MessageInspectionBehavior());
    client.HelloWorld();
    

    代码:

    class MessageInspectionBehavior : IClientMessageInspector, IEndpointBehavior
    {
        public void Validate(ServiceEndpoint endpoint)
        {
        }
    
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }
    
        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
        }
    
        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            clientRuntime.MessageInspectors.Add(this);
        }
    
        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            //Write request message
            Console.WriteLine(request.ToString());
            return null;
        }
    
        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            // Write out http headers
            foreach (var property in reply.Properties)
            {
                if (!(property.Value is HttpResponseMessageProperty)) continue;
                var httpProperties = (HttpResponseMessageProperty)property.Value;
                foreach (KeyValuePair<object, object> kvp in httpProperties.Headers)
                {
                    Console.WriteLine(kvp.Key + ":" + kvp.Value);
                }
            }
            // Write result message
            Console.WriteLine(reply.ToString());
        }
    }
    

    同样,您可以使用IDispatchMessageInspector和ISeviceBehavior在服务端编写一个记录器。

        2
  •  1
  •   DarthJDG chetan borse    13 年前

    喜欢它,帮助了很多。但在未来,你可能想告诉我们 using 我们可能需要的陈述。

    对于其他人,您需要包括:

    Using System.ServiceModel;
    Using System.ServiceModel.Dispatcher;
    Using System.ServiceModel.Channels;
    Using System.ServiceModel.Description;