How to set HTTP Headers from client class inherited from SoapHttpClientProtocol

3.4k Views Asked by At

I'm using a class MyClass inherited from SoapHttpClientProtocol (auto-generated in my project by creating a WebReference from a .wsdl file, representing a service).

Before calling a "WebMethod" of this service, I need to custom the http header of my request. I tried overloading the GetWebRequest() method of SoapHttpClientProtocol that way :

public partial class MyClass: System.Web.Services.Protocols.SoapHttpClientProtocol{

 protected override WebRequest GetWebRequest(Uri uri) {

            HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(uri);

            request.Headers.Add("MyCustomHeader", "MyCustomHeaderValue");

            return request;

        }
    }

I was hoping that GetWebRequest was called in the constructor of MyClass, apparently it's not.

Could someone help me ?

2

There are 2 best solutions below

0
On

GetWebRequest is called when the proxy needs to get a web request.

0
On

Right, the GetWebRequest virtual should be called each time a web method is invoked.