如何HTTP标头添加到SOAP客户端
问题描述:
有人能回答我,如果有可能的HTTP标头添加到SOAP客户端的Web服务调用。
冲浪互联网之后的唯一薄我发现了如何添加SOAP头
Can someone answer me if it is possible to add HTTP header to soap client web-service calls. After surfing Internet the only thin I found was how to add SOAP header.
中的代码如下:
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属性应该是空的。
Header property in the envelop should be empty
答
尝试使用这样的:
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();
}