SAAJ返回" 1字节UTF-8"的无效字节1

时间:2013-11-28 16:53:05

标签: java soap encoding utf-8 saaj

我与HP Service Manager编写SOAP连接并使用SAAJ。毕竟,建立连接但是当结果出现时我收到错误:

    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)

... ...

在输出之前编码结果的任何方法吗?

这就是代码:

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);
    }

}

提前致谢!

1 个答案:

答案 0 :(得分:0)

我不确定这是你的问题,但我已经遇到了内容类型的问题。 应该写:

headers.addHeader("Content-Type", "text/xml; charset=UTF-8");

而不是

headers.addHeader("Content-Type", "text/xml;charset=UTF-8");