title: How to add custom SOAP header for C# Web service client invoke Axis 1.4 Web service
body: <p>I'm trying to write a C# application that's a Webservice client that invokes a Java Web service (using Axis 1.4), but I can't get it to work.
  Java WS requires a custom SOAP header for authentication, so I added one to my C# application. The code generated in VS 2010 doesn't include a SOAP header, so I add a <code>MySOAPHeader</code> class into the project:</p>
  
  <pre><code>namespace AddProvinceCityDemo
  {
   [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://service.itthrm.vnitt.org")]
      [System.Xml.Serialization.XmlRootAttribute("securitytoken", Namespace = "http://service.itthrm.vnitt.org", IsNullable = false)]
      public class MySOAPHeader:SoapHeader
      {
          public String username;
          public String password;
      }
  }
  </code></pre>
  
  <p>I also altered the generated code in Reference.cs by adding a <code>MySOAPHeader</code> variable </p>
  
  <pre><code>public partial class ProvinceServiceClient : System.ServiceModel.ClientBase&lt;AddProvinceCityDemo.ProvinceService.ProvinceService&gt;, AddProvinceCityDemo.ProvinceService.ProvinceService {
  
          public MySOAPHeader customSOAPHeader;
          public ProvinceServiceClient() {
          }
           //.........................
  }
  </code></pre>
  
  <p>Finally, I invoked WS with the following code:</p>
  
  <pre><code>MySOAPHeader authentication = new MySOAPHeader();
   authentication.username = username;
  
    authentication.password = encryptedpass;
   //end set SOAP header information
  
   ProvinceServiceClient provinceservice = new ProvinceServiceClient();
  
   provinceservice.customSOAPHeader = authentication;
   Province[] arrprovince = null;
  
   arrprovince = provinceservice.findAll();
  </code></pre>
  
  <p>But it ends up throwing an error: <code>"Failed to retrieve the SOAP header"</code>.</p>
  
  <p>It was easy to write a Java version of the code:</p>
  
  <pre><code>SOAPHeaderElement oHeaderElement;
  
  oHeaderElement = new SOAPHeaderElement(namespace, "securityHeader");
  oHeaderElement.setPrefix"sec");
  oHeaderElement.setMustUnderstand(false);
  oElement = oHeaderElement.addChildElement("username");
  oElement.addTextNode(username);
  oElement = oHeaderElement.addChildElement("password");
  oElement.addTextNode(passwordEncrypt); 
  </code></pre>
  
  <p>How can I do the same in .NET and C#?</p>