title: Add a header on Web Service request using CXF
body: <p>How do you add a header on Web Service client request that looks like below XML?<br>
  The xml below is a request auto-generated by SOAP UI and its working fine given the correct username/password. </p>
  
  <pre><code>&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:dof="http://dof.ad.com"&gt;
     &lt;soapenv:Header&gt;
        &lt;dof:UserCredentials&gt;
           &lt;!--Optional:--&gt;
           &lt;dof:userName&gt;&lt;/dof:userName&gt;
           &lt;!--Optional:--&gt;
           &lt;dof:password&gt;&lt;/dof:password&gt;
        &lt;/dof:UserCredentials&gt;
     &lt;/soapenv:Header&gt;
     &lt;soapenv:Body&gt;
        &lt;dof:CheckService/&gt;
     &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>I'd like to know how the header portion can be added using apache CXF. I've auto-generated a code using CXF from WSDL (see below) and checked various articles but the code is still not working. I am assuming the request being sent is not correct or it doesn't have a header. </p>
  
  <pre><code>public static void checkServiceMan() {
      String address = "https://WSDL_URL";
  
      JaxWsProxyFactoryBean jaxWsProxy = new JaxWsProxyFactoryBean();
      jaxWsProxy.setServiceClass(DOFairservice.class);
      jaxWsProxy.setAddress(address);
  
      DOFairservice serviceClient = (DOFairservice) jaxWsProxy.create();
  
      ObjectFactory factory = new ObjectFactory();
      UserCredentials uc = factory.createUserCredentials();
      uc.setUserName("username");
      uc.setPassword("password");
  
      List&lt;Header&gt; headerList = new ArrayList&lt;Header&gt;();
      try {
  
          Header testCredentialsHeader = new Header(new QName("http://DOF", "DOFairservice")
                  ,uc
                  ,new JAXBDataBinding(UserCredentials.class));
  
          headerList.add(testCredentialsHeader);
  
      } catch (JAXBException e) {
  
          e.printStackTrace();
      }
      ((BindingProvider) serviceClient).getResponseContext().put(Header.HEADER_LIST, headerList);
  
      Client client = ClientProxy.getClient(serviceClient);
      HTTPConduit conduit = (HTTPConduit) client.getConduit();
      HTTPClientPolicy policy = conduit.getClient();
      policy.setReceiveTimeout(10000);
  
      String resp = serviceClient.checkService(); **&lt;--- ERROR HERE**
      System.out.println("Response from Customs Web Service: "+ resp);
  
      System.out.println("Web Service Header Test Done");
  }
  </code></pre>
  
  <p>Error Message</p>
  
  <pre><code>Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Error reading XMLStreamReader: Unexpected character '8' (code 56) in prolog; expected '&lt;'
   at [row,col {unknown-source}]: [1,1]
      at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:161)
      at com.sun.proxy.$Proxy45.checkService(Unknown Source)
      at ae.abudhabi.dof.client.CheckServiceClient.checkServiceMan(CheckServiceClient.java:103)
      at ae.abudhabi.dof.client.CheckServiceClient.main(CheckServiceClient.java:112)
  Caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '8' (code 56) in prolog; expected '&lt;'
  </code></pre>