在Visual Studio 2008中向Soap请求添加Soap头

3
我正在尝试使用第三方Web服务(因此我无法访问Web服务代码)。 在Visual Studio 2008中,我创建了一个新的Web站点项目(ASP和c#),并添加了Web引用(不是Web服务!所以我猜它不是WCF服务...正确吗?)。
问题在于,从Web服务文档中我知道每个SOAP请求都必须使用以下信封和头部发送,请告诉我如何在我的SOAP请求中添加这些内容? 我找到的所有解决方案都需要修改Web服务源或代理,但我不能这样做,因为我没有访问Web服务源和Visual Studio 2008客户端中的Web服务代理只存在于只读临时文件中!
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<soap:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" soap:mustUnderstand="1">
<wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:Username>gimme.data@stats.com</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">Ima5tatto</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soap:Header>
<soap:Body xmlns:ns2="http://neighbourhood.statistics.gov.uk/nde/v1-0/discoverystructs">
<ns2:AreaAtLevelElement>
<AreaIdWithLevelType>
<AreaId>276704</AreaId>
<LevelTypeId>12</LevelTypeId>
</AreaIdWithLevelType>
</ns2:AreaAtLevelElement>
</soap:Body>
</soap:Envelope>
2个回答

3
您可以在配置文件中使用endpoint元素中的headers元素静态地添加消息头。headers元素的每个子元素都将原样复制到消息头中。

0

我正在为这个问题苦苦挣扎,到目前为止,我已经编写了一个消息检查器以便能够访问SOAP头,尽管我不确定如何在没有手动操作的情况下将wsse:security内容添加进去。我想使用WS-Security模式(以及SAML模式)来构建wsse:security内容...

值得一提的是,如果我解决了这个问题,我会在这个帖子上发布我的代码。

以下是我向客户端添加行为的代码:

client.Endpoint.Behaviors.Add(new CustomBehavior());
msgOutput = client.ProvideAndRegisterDocumentSetXDR(msgInput);

这是消息检查器和自定义行为:

public class CustomMessageInspector : System.ServiceModel.Dispatcher.IClientMessageInspector
{
    public void AfterReceiveReply(ref WCF.Message reply, object correclationState)
    {
    }

    public Object BeforeSendRequest(ref WCF.Message request, IClientChannel channel)
    {
        MessageHeaders headers = new MessageHeaders(MessageVersion.Soap11WSAddressing10);
        MessageHeader header = MessageHeader.CreateHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "");
        request.Headers.Add(header);
        return null;
    }
}


public class CustomBehavior : System.ServiceModel.Description.IEndpointBehavior
    {
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRunTime)
        {
            CustomMessageInspector inspector = new CustomMessageInspector();
            clientRunTime.MessageInspectors.Add(inspector);
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }
    }

1
请阅读FAQ。这不是一个讨论论坛。请提出您自己的问题 - 我们不会在这里“回复帖子”。此外,如果没有注意到他正在使用“Web引用”,则为-1。 - John Saunders

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接