20

如果可以将 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 属性应为空

4

5 回答 5

53

尝试使用这个:

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();
}

有关更多详细信息,请参见此处

于 2014-04-10T20:37:29.197 回答
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();
}
于 2013-09-19T06:20:02.853 回答
1
var client = new MyServiceSoapClient();
using (new OperationContextScope(InnerChannel))
{ 
    WebOperationContext.Current.OutgoingRequest.Headers.Add("myCustomHeader", "myValue");                
}
于 2013-09-19T06:34:04.977 回答
0

其中一些答案会将标头添加到请求的 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 请求和响应的上下文属性的轻松访问。

于 2021-08-02T19:05:26.403 回答
0

要将 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;
}}

接下来我们需要将此 MessageInspector 添加到 clientRuntime.MessageInspectors 点的 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”,其值为我们在代码中指定的“我们想要的实际值”。

于 2022-02-17T19:25:31.437 回答