title: SAAJ return "Invalid byte 1 of 1-byte UTF-8"
body: <p>I write SOAP connection with HP Service Manager and use SAAJ. After all, connection is made but when result is comming i receive a error: </p>
  
  <pre><code>    Response SOAP Message = ERROR:  'Invalid byte 1 of 1-byte UTF-8 sequence.'
      Error occurred while sending SOAP Request to Server
      javax.xml.transform.TransformerException: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
  
  Error occurred while sending SOAP Request to Server
  javax.xml.transform.TransformerException: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
      at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:735)
      at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:336)
      at SOAPClientSAAJ.printSOAPResponse(SOAPClientSAAJ.java:114)
      at SOAPClientSAAJ.main(SOAPClientSAAJ.java:22)
  Caused by: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
      at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:684)
  </code></pre>
  
  <p>...
  ...</p>
  
  <p>Any way to encode result before output?</p>
  
  <p>That is the code: </p>
  
  <pre><code>public class SOAPClientSAAJ {
  
  
      /**
       * Starting point for the SAAJ - SOAP Client Testing
       */
      public static void main(String args[]) {
          try {
              // Create SOAP Connection
              SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
              SOAPConnection soapConnection = soapConnectionFactory.createConnection();
  
              // Send SOAP Message to SOAP Server
              String url = "http://server:13080/SM/7";
              SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
  
              // Process the SOAP Response
              printSOAPResponse(soapResponse);
  
              soapConnection.close();
          } catch (Exception e) {
              System.err.println("Error occurred while sending SOAP Request to Server");
              e.printStackTrace();
          }
      }
  
      private static SOAPMessage createSOAPRequest() throws Exception {
          MessageFactory messageFactory = MessageFactory.newInstance();
          SOAPMessage soapMessage = messageFactory.createMessage();
          SOAPPart soapPart = soapMessage.getSOAPPart();
  
  
  
          String username= "user";
          String password= "pass";
  
          String serverURI = "http://server:13080/SM/7";
  
          // SOAP Envelope
          SOAPEnvelope envelope = soapPart.getEnvelope();
          envelope.addNamespaceDeclaration("pws", serverURI);
  
          // SOAP Body
          SOAPBody soapBody = envelope.getBody();
  
  
          SOAPElement soapBodyElem = soapBody.addChildElement("RetrieveContactListRequest", "pws");
          soapBodyElem.setAttribute("filename", "Contacts");
          soapBodyElem.setAttribute("attachmentInfo", "false");
          soapBodyElem.setAttribute("attachmentData", "false");
          soapBodyElem.setAttribute("ignoreEmptyElements", "true");
          soapBodyElem.setAttribute("updatecounter", "");
          soapBodyElem.setAttribute("handle", "");
          soapBodyElem.setAttribute("count", "");
          soapBodyElem.setAttribute("start", "");
  
  
          SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("keys", "pws");
          soapBodyElem1.setAttribute("query", "true");
          soapBodyElem1.setAttribute("updatecounter", "false");
          SOAPElement soapBodyElem2 = soapBodyElem1.addChildElement("ContactName", "pws");
          soapBodyElem2.setAttribute("type", "String");
          soapBodyElem2.setAttribute("mandatory", "");
          soapBodyElem2.setAttribute("readonly", "");
  
          MimeHeaders headers = soapMessage.getMimeHeaders();
          String authorization = new sun.misc.BASE64Encoder().encode((username+":"+password).getBytes());
          headers.addHeader("Accept-Encoding", "gzip,deflate");
          headers.addHeader("Authorization", "Basic " + authorization);
          headers.addHeader("SOAPAction", "RetrieveList");
          headers.addHeader("Content-Type", "text/xml;charset=UTF-8");
          //headers.addHeader("Operation:", "RetrieveContactList");
          headers.addHeader("RetrieveContactList", "http://server:13080/SM/7/ConfigurationManagement.wsdl");
  
          soapMessage.saveChanges();
  
          /* Print the request message */
          System.out.print("Request SOAP Message = ");
          soapMessage.writeTo(System.out);
          System.out.println();
  
          return soapMessage;
      }
  
      /**
       * Method used to print the SOAP Response
       */
      private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
          TransformerFactory transformerFactory = TransformerFactory.newInstance();
          Transformer transformer = transformerFactory.newTransformer();
          Source sourceContent = soapResponse.getSOAPPart().getContent();
          System.out.print("\nResponse SOAP Message = ");
          StreamResult result = new StreamResult(System.out);
          transformer.transform(sourceContent, result);
      }
  
  }
  </code></pre>
  
  <p>Thanks in advance! </p>