How to add HTTP Header to SOAP Client

前端 未结 3 1819
别那么骄傲
别那么骄傲 2020-12-01 18:32

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.

相关标签:
3条回答
  • 2020-12-01 18:49

    Try to use this:

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

    See here for more detail.

    0 讨论(0)
  • Try this

    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();
    }
    
    0 讨论(0)
  • 2020-12-01 19:12
    var client = new MyServiceSoapClient();
    using (new OperationContextScope(InnerChannel))
    { 
        WebOperationContext.Current.OutgoingRequest.Headers.Add("myCustomHeader", "myValue");                
    }
    
    0 讨论(0)
提交回复
热议问题