【问题标题】:Add Soap Header to Soap request in Visual Studio 2008在 Visual Studio 2008 中将 Soap 标头添加到 Soap 请求
【发布时间】:2009-06-18 11:00:13
【问题描述】:

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

问题是,从网络服务的文档中我知道每个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>

【问题讨论】:

    标签: c# visual-studio-2008 web-services soap asmx


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我正在为同样的问题而苦苦挣扎,到目前为止,我已经编写了一个 message inspector 以便能够访问 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)
              {
              }
          }
      

      【讨论】:

      • 阅读FAQ。这不是一个讨论论坛。问你自己的问题 - 我们在这里不“回复主题”。此外,-1 表示没有注意到他正在使用“网络参考”。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      • 2015-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      相关资源
      最近更新 更多