title: ServerSOAPFaultException: Client received SOAP Fault from server
body: <p>I have a wsdl from ERP cloud. To consume it in Java, I compiled it using <code>wsimport</code> and extracted all the java files.</p>
  <p>Now I'm trying to use these java files in order to communicate to the server data through wsdl. The wsdl requires authentication.</p>
  <p><strong>Problem</strong></p>
  <p>Whenever I try to run my custom code to connect to wsdl, for authentication it works with simple setting of username and password in Binding context.</p>
  <pre><code>private void attachAuthentication(Object className) {
      BindingProvider prov = (BindingProvider) className;
      prov.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, &quot;xxx&quot;);
      prov.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, &quot;xxx&quot;);
  }
  </code></pre>
  <p>But whenever I deploy my code on a weblogic GPI instance, it gives me following error.</p>
  <pre><code>com.sun.xml.ws.fault.ServerSOAPFaultException: Client received SOAP Fault from server: The request was invalid or malformed Please see the server log to find more detail regarding exact cause of the failure.
      at com.sun.xml.ws.fault.SOAP12Fault.getProtocolException(SOAP12Fault.java:229)
      at com.sun.xml.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:139)
      at com.sun.xml.ws.client.sei.StubHandler.readResponse(StubHandler.java:253)
      at com.sun.xml.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:203)
      at com.sun.xml.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:290)
      at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:119)
  </code></pre>
  <p><a href="https://i.stack.imgur.com/WlJn3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WlJn3.png" alt="enter image description here" /></a>
  I tried attaching a Soap handler to my SEI. Here's what I've done.</p>
  <p><strong>InitializeErp.java</strong></p>
  <pre><code>HeaderHandlerResolver handlerResolver = new HeaderHandlerResolver();
  
  InvoiceInterfaceService_Service interfaceInvoice = new InvoiceInterfaceService_Service();
  interfaceInvoice.setHandlerResolver(handlerResolver);
  invoiceInterfaceServiceSoapHttpPort = interfaceInvoice.getInvoiceInterfaceServiceSoapHttpPort();
  </code></pre>
  <p><strong>HeaderHandlerResolver.java</strong></p>
  <pre><code>public class HeaderHandlerResolver implements HandlerResolver {
  
      @Override
      public List&lt;Handler&gt; getHandlerChain(PortInfo portInfo) {
          List&lt;Handler&gt; handlerChain = new ArrayList&lt;Handler&gt;();
  
          HandlerHeader hh = new HandlerHeader();
  
          handlerChain.add(hh);
  
          return handlerChain;
      }
  }
  </code></pre>
  <p><strong>HeaderHandler.java</strong></p>
  <pre><code>public class HandlerHeader implements SOAPHandler&lt;SOAPMessageContext&gt; {
  
  
      @Override
      public Set&lt;QName&gt; getHeaders() {
          return Collections.emptySet();
      }
  
      @Override
      public boolean handleMessage(SOAPMessageContext context) {
          final Boolean outInd = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
  
          if (outInd) {
              SimpleDateFormat format = new SimpleDateFormat(&quot;yyyy-MM-dd'T'HH:mm:ss.mmm'Z'&quot;);
  
              Date creationDate = new Date();
              Date expirationDate = new Date(creationDate.getTime() + TimeUnit.HOURS.toMillis(1));
              try {
  
                  SOAPMessage message = context.getMessage();
                  SOAPEnvelope env = message.getSOAPPart().getEnvelope();
                  env.addNamespaceDeclaration(&quot;sch&quot;, &quot;http://xmlns.oracle.com/scheduler&quot;);
                  env.addNamespaceDeclaration(&quot;typ&quot;, &quot;http://xmlns.oracle.com/scheduler/types&quot;);
  
                  SOAPHeader header = env.getHeader();
                  SOAPBody body = env.getBody();
  
                  env.removeNamespaceDeclaration(&quot;SOAP-ENV&quot;);
  
                  env.setPrefix(&quot;soapenv&quot;);
                  header.setPrefix(&quot;soapenv&quot;);
                  body.setPrefix(&quot;soapenv&quot;);
  
                  QName security = new QName(&quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd&quot;, &quot;Security&quot;, &quot;wsse&quot;);
                  // Constructing Header
                  SOAPElement securityElement = header.addChildElement(security);
                  securityElement.addNamespaceDeclaration(&quot;wsu&quot;, &quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd&quot;);
                  securityElement.setAttribute(&quot;soapenv:mustUnderstand&quot;, &quot;1&quot;);
  
                  QName usernameToken = new QName(&quot;UsernameToken&quot;, &quot;wsse&quot;);
                  SOAPElement usernameTokenElement = securityElement.addChildElement(&quot;UsernameToken&quot;, &quot;wsse&quot;);
                  usernameTokenElement.setAttribute(&quot;wsu:Id&quot;, &quot;UsernameToken-97B1FF404874F4997215144527824364&quot;);
  
                  QName username = new QName(&quot;Username&quot;, &quot;wsse&quot;);
                  SOAPElement usernameElement = usernameTokenElement.addChildElement(&quot;Username&quot;, &quot;wsse&quot;);
                  usernameElement.addTextNode(&quot;xxx&quot;);
  
                  QName passwrod = new QName(&quot;Password&quot;, &quot;wsse&quot;);
                  SOAPElement passwordElement = usernameTokenElement.addChildElement(&quot;Password&quot;, &quot;wsse&quot;);
                  passwordElement.setAttribute(&quot;Type&quot;, &quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText&quot;);
                  passwordElement.addTextNode(&quot;xxx&quot;);
  
                  QName nonce = new QName(&quot;Nonce&quot;, &quot;wsse&quot;);
                  SOAPElement nonceElement = usernameTokenElement.addChildElement(&quot;Nonce&quot;, &quot;wsse&quot;);
                  nonceElement.setAttribute(&quot;EncodingType&quot;, &quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary&quot;);
                  nonceElement.addTextNode(&quot;Ox0BI3sh1pPSe2S7NsUzeg==&quot;);
  
                  QName created = new QName(&quot;Created&quot;, &quot;wsu&quot;);
                  SOAPElement createdElement = usernameTokenElement.addChildElement(&quot;Created&quot;, &quot;wsu&quot;);
                  createdElement.addTextNode(format.format(creationDate));
  
                  QName timestamp = new QName(&quot;Timestamp&quot;, &quot;wsu&quot;);
                  SOAPElement timestampElement = securityElement.addChildElement(&quot;Timestamp&quot;, &quot;wsu&quot;);
                  timestampElement.setAttribute(&quot;wsu:Id&quot;, &quot;TS-4&quot;);
  
                  QName createdTimestamp = new QName(&quot;Created&quot;, &quot;wsu&quot;);
                  SOAPElement createdTimestampElement = timestampElement.addChildElement(&quot;Created&quot;, &quot;wsu&quot;);
                  createdTimestampElement.addTextNode(format.format(creationDate));
  
                  QName expiresTimestamp = new QName(&quot;Expires&quot;, &quot;wsu&quot;);
                  SOAPElement expiresTimestampElement = timestampElement.addChildElement(&quot;Expires&quot;, &quot;wsu&quot;);
                  expiresTimestampElement.addTextNode(format.format(expirationDate));
                  // Print out the outbound SOAP message to System.out
                  System.out.println(&quot;setting attributes...&quot;);
                  message.writeTo(System.out);
  
                  System.out.println(&quot;&quot;);
                  System.out.println(&quot;message printed..&quot;);
  
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
          return true;
      }
  
      @Override
      public boolean handleFault(SOAPMessageContext context) {
  
          try {
              SOAPMessage message = context.getMessage();
              ByteArrayOutputStream out = new ByteArrayOutputStream();
              message.writeTo(out);
              String strMsg = new String(out.toByteArray());
              RequestFilter.getSession().setAttribute(&quot;soap fault&quot;, &quot;Soap -&gt;&quot; + strMsg);
          } catch (SOAPException | IOException ex) {
              RequestFilter.getSession().setAttribute(&quot;mailContent&quot;, ex.toString());
          }
          return true;
      }
  
      @Override
      public void close(MessageContext context) {
      }
  }
  </code></pre>
  <p>This is what my Soap HEader looks like.</p>
  <p><a href="https://i.stack.imgur.com/3uP9y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3uP9y.png" alt="enter image description here" /></a></p>
  <p>On running it on server. This is what I get in my SoapFault</p>
  <pre><code>Soap  -&gt; ns1:Senderns2:InvalidRequestThe request was invalid or malformedweblogic.wsee.security.wst.faults.InvalidRequestException: Server Authentication Required at weblogic.wsee.security.wst.framework.TrustSoapClient.invoke(TrustSoapClient.java:157) at weblogic.wsee.security.wst.framework.TrustSoapClient.requestTrustToken(TrustSoapClient.java:110) at weblogic.wsee.security.saml.SAMLTrustCredentialProvider.createCredential(SAMLTrustCredentialProvider.java:432) at 
  </code></pre>
  <p><a href="https://i.stack.imgur.com/6LG6j.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6LG6j.png" alt="enter image description here" /></a></p>
  <p>I've many ways to modify the Soap header, Removed security, just passed username password, Added both handler as well as credentials in BindingContext, But nothing seems to work on server.</p>
  <p>With the header attached, I get the following error on local<a href="https://i.stack.imgur.com/FEPwB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FEPwB.png" alt="enter image description here" /></a></p>
  <p>I'm out of ways now. Can anyone please suggest me any approach or point out what am I missing. What should I try now. Any help is appreciated. All suggestions are welcome.</p>