在 Visual Studio 2008 中将 Soap 标头添加到 Soap 请求

发布于 2024-07-24 10:07:08 字数 1769 浏览 2 评论 0原文

我正在尝试使用第三方网络服务(因此我无权访问网络服务代码)。 在 Visual Studio 2008 中,我创建了一个新的网站项目(ASP 和 C#),并添加了 Web 引用(不是 Web 服务!所以我猜它不是 WCF 服务...对吗?)。

问题是,从 Web 服务的文档中我知道每个肥皂请求都必须使用以下信封和标头发送,您能告诉我如何将其添加到我的肥皂请求中吗? 我发现的所有解决方案都需要修改 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>[email protected]</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>

I'm trying to use a third party web service (so I do not have access to the web service code).
In Visual Studio 2008 I created a new web site project (ASP and c#), and added the web reference (not web service! so I guess it is not a WCF service... correct?).

The problem is that from the documentation of the web service I know that each soap request has to be sent with the following envelope and header, can you please tell me how to add this in my Soap requests?
All the solutions I found required modifying the web service source or proxy, I can't do that because I don't have access to the web service source and the web service proxy in the client in Visual studio 2008 comes in a read-only temp file!

<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>[email protected]</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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终止放荡 2024-07-31 10:07:08

您可以使用标头endpoint 元素中的 > 元素。 headers 元素的每个子元素都将按邮件标头中的原样进行复制。

You can statically add headers to the messages in the configuration file using the headers element in the endpoint element. Each child element of the headers element will be copied as is in the header of your message.

错々过的事 2024-07-31 10:07:08

我正在努力解决同样的问题,到目前为止已经编写了一个 消息检查器 能够获取 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)
        {
        }
    }

I'm struggling with this same issue and thus far have written a message inspector to be able to get to the SOAP headers, although I'm not sure how to get the wsse:security stuff in there without having to do it manually. I would like to be able to use the WS-Security schema (as well as SAML schemas) to build the wsse:security stuff...

For what it's worth my message inspector code is below, if i solve this I'll post it on this thread.

Here's where I add the behavior to the client:

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

And here is the message inspector and custom behavior:

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)
        {
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文