C# 在VisualStudio2008中将Soap头添加到Soap请求

C# 在VisualStudio2008中将Soap头添加到Soap请求,c#,visual-studio-2008,web-services,soap,asmx,C#,Visual Studio 2008,Web Services,Soap,Asmx,我正在尝试使用第三方web服务(因此我无法访问web服务代码)。 在VisualStudio2008中,我创建了一个新的web站点项目(ASP和c#),并添加了web引用(不是web服务!所以我猜它不是WCF服务…正确吗?) 问题是,从web服务的文档中,我知道每个soap请求都必须使用以下信封和标题发送,您能告诉我如何将其添加到我的soap请求中吗? 我找到的所有解决方案都需要修改web服务源或代理,我不能这样做,因为我没有访问web服务源的权限,并且Visual studio 2008中客户

我正在尝试使用第三方web服务(因此我无法访问web服务代码)。 在VisualStudio2008中,我创建了一个新的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>

给我。data@stats.com
Ima5tatto
276704
12

您可以使用元素中的元素向配置文件中的消息静态添加标题。headers元素的每个子元素都将按照消息头中的原样进行复制。

我正在努力解决同样的问题,到目前为止,我已经写了一篇文章,以便能够访问SOAP头,尽管我不知道如何在不必手动操作的情况下将wsse:security内容放在其中。我希望能够使用WS-Security模式(以及SAML模式)来构建wsse:Security内容

值得一提的是,我的message inspector代码如下,如果我解决了这个问题,我会将它发布在这个线程上

下面是我向客户端添加行为的地方:

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,因为他没有注意到他在使用“网络参考”。