加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 08:13:42 所属栏目:百科 来源:网络整理
导读:我有一个简单的c#3.5 .Net控制台应用程序,它连接到服务引用.一切正常 – 打电话和接收回复,但现在我被告知要查看消息中的Soap标题. 我发现.Net WebService Studio非常棒,并且会显示Soap请求和Soap响应. 对于响应,它显示如下: ResponseCode: 200 (OK)Content
我有一个简单的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>

如何在我的应用程序中生成类似的东西?

我有兴趣看到的响应是一个不同的方法,它返回一个足够大的消息来炸毁WebService Studio.我没有看到如何使用此工具设置邮件大小参数.所以,我想自己捕获这些信息.

有关如何做到这一点的任何想法?

解决方法

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和IServiceBehavior在服务端编写记录器.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读