【问题标题】:How to add HTTP Header to SOAP Client如何将 HTTP 标头添加到 SOAP 客户端
【发布时间】:2013-09-24 01:32:54
【问题描述】:

如果可以将 HTTP 标头添加到肥皂客户端 Web 服务调用中,有人可以回答我吗? 上网后,我唯一发现的就是如何添加 SOAP 标头。

代码如下:

var client =new MyServiceSoapClient();
//client.AddHttpHeader("myCustomHeader","myValue");//There's no such method, it's just for clearness
var res = await client.MyMethod();

更新:

The request should look like this
POST https://service.com/Service.asmx HTTP/1.1
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://www.host.com/schemas/Authentication.xsd/Action"
Content-Length: 351
MyHeader: "myValue"
Expect: 100-continue
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header/>
  <s:Body>
    <myBody>BodyGoesHere</myBody>
  </s:Body>
</s:Envelope>

信封中的Header属性应该为空

【问题讨论】:

    标签: c# windows-8 soap-client


    【解决方案1】:

    尝试使用这个:

    SoapServiceClient client = new SoapServiceClient();
    
    using(new OperationContextScope(client.InnerChannel)) 
    {
        // // Add a SOAP Header (Header property in the envelope) to an outgoing request. 
        // MessageHeader aMessageHeader = MessageHeader
        //    .CreateHeader("MySOAPHeader", "http://tempuri.org", "MySOAPHeaderValue");
        // OperationContext.Current.OutgoingMessageHeaders.Add(aMessageHeader);
    
        // Add a HTTP Header to an outgoing request
        HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
        requestMessage.Headers["MyHttpHeader"] = "MyHttpHeaderValue";
        OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] 
           = requestMessage;
    
        var result = client.MyClientMethod();
    }
    

    更多详情请参阅here

    【讨论】:

    • 这应该被标记为答案,为我工作。谢谢
    • 如果 InnerChanner 在当前客户端中不存在怎么办(我正在尝试使用 SOAP 调用)
    • 什么是SoapServiceClient
    • 在我的例子中,InnerChannel 不存在,因为我使用 WSDL.exe 创建客户端代理,而不是 svcutil.exe
    • 工作就像一个魅力,让我免于头痛。感谢您留下注释代码,这实际上就是我想要的 - 在信封内添加标题。
    【解决方案2】:

    试试这个

    var client = new MyServiceSoapClient();
    using (var scope = new OperationContextScope(client.InnerChannel))
    {
        // Create a custom soap header
        var msgHeader = MessageHeader.CreateHeader("myCustomHeader", "The_namespace_URI_of_the_header_XML_element", "myValue");
        // Add the header into request message
        OperationContext.Current.OutgoingMessageHeaders.Add(msgHeader);
    
        var res = await client.MyMethod();
    }
    

    【讨论】:

    • 谢谢,这不是我需要的。我已经更新了描述,看看它。
    • 这看起来不错,但它可以编码。例如 &amp;lt; 变为 &amp;lt;。任何想法如何防止这种情况?我检查了重载,但似乎没有一个按原样添加标题,RAW。
    【解决方案3】:
    var client = new MyServiceSoapClient();
    using (new OperationContextScope(InnerChannel))
    { 
        WebOperationContext.Current.OutgoingRequest.Headers.Add("myCustomHeader", "myValue");                
    }
    

    【讨论】:

    • 谢谢,这不是我需要的。我已经更新了描述,看看它。
    • 什么是MyServiceSoapClient
    • 如果您尝试在 RequestHeader 中传递客户标头,那么这将是正确的答案。对我来说就是这样。
    【解决方案4】:

    其中一些答案会在请求的 XML 正文内容中添加标头。要将标头添加到请求本身,请执行以下操作:

    SoapServiceClient client = new SoapServiceClient();
    
    using(var scope = new OperationContextScope(client.InnerChannel)) 
    {
         WebOperationContext.Current.OutgoingRequest.Headers.
               Add("headerKey", "headerValue");
    
        var result = client.MyClientMethod();
    }
    

    注意WebOperationContext的OperationContext的变化。是一个帮助类,可让您轻松访问 Web 请求和响应的上下文属性。

    【讨论】:

      【解决方案5】:

      要将 HTTP 标头添加到标准生成的 SOAP 客户端(通过服务引用或使用 svcutil 生成客户端),您可以利用我认为最方便的 Endpoint.Behaviors 扩展点。

      需要完成几个步骤。首先,我们需要创建具有实际功能的消息检查器,它将向每个 SOAP 请求添加 HTTP 标头(“HEADER_WHICH_WE_WANT”)

      using System.ServiceModel;
      using System.ServiceModel.Channels;
      using System.ServiceModel.Dispatcher;
      
      class MessageInspector : IClientMessageInspector
      {
        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
          var property = request.Properties.ContainsKey(HttpRequestMessageProperty.Name)?
          request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty: new HttpRequestMessageProperty();
      
          if (null == property)
            return null;
      
          property.Headers["HEADER_WHICH_WE_WANT"] = "Actual Value we want";
          request.Properties[HttpRequestMessageProperty.Name] = property;
          return null;
      }}
      

      接下来我们需要在clientRuntime.MessageInspectors点将这个MessageInspector添加到IEndpointBehavior接口实现

      using System.ServiceModel.Channels;
      using System.ServiceModel.Description;
      using System.ServiceModel.Dispatcher;
      
      internal class InspectorBehavior : IEndpointBehavior
      {
        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
         clientRuntime.MessageInspectors.Add(new MessageInspector());
        }
      }
      

      最后一件事是为 SOAP 客户端注册此行为

      SoapClient.Endpoint.Behaviors.Add(new InspectorBehavior());
      

      就是这样,现在每个 SOAP 调用都将配备自定义 HTTP 标头“HEADER_WHICH_WE_WANT”,其值为我们在代码中指定的“我们想要的实际值”。

      【讨论】:

        猜你喜欢
        • 2011-05-02
        • 1970-01-01
        • 2011-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多