如何将 HTTP 标头添加到 SOAP 客户端
本文关键字:添加 SOAP 客户端 HTTP | 更新日期: 2023-09-27 18:31:49
如果可以将HTTP标头添加到soap客户端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>
信封中的标头属性应为空
尝试使用这个:
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();
}
有关更多详细信息,请参阅此处。
试试这个
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();
}
var client = new MyServiceSoapClient();
using (new OperationContextScope(InnerChannel))
{
WebOperationContext.Current.OutgoingRequest.Headers.Add("myCustomHeader", "myValue");
}
要将 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 的 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",其值为我们在代码中指定的值"我们想要的实际值"。
其中一些
答案会将标头添加到请求的 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 请求和响应的上下文属性的轻松访问。