0
我必須從字符串中創建一個SOAPMessage。生成信封特殊字符的問題
我的問題是,該字符串包含特殊字符,並在嘗試生成信封時給我一個錯誤。
我必須用特殊字符創建信封。
我只使用java 1.7的類,我需要使用代碼的類,我不能使用另一種類型的信封。
這是我的代碼
public class Main {
public static void main(String[] args) {
try {
//We have a envelope with special characters
String input = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header/><soapenv:Body xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\"><car:Input xmlns:car=\"http://soap.sctinst.redsys.es/consumed/CARGAENTIDADES\"><list><obj><name>José Televisión</name></obj></list></car:Input></soapenv:Body></soapenv:Envelope>";
MimeHeaders headers = new MimeHeaders();
headers.setHeader("Content-Type", "text/xml");
headers.setHeader("Encoding", "UTF-8");
ByteArrayInputStream bis = new ByteArrayInputStream(input.getBytes("UTF-8"));
int n = bis.available();
byte[] bytes = new byte[n];
bis.read(bytes, 0, n);
//I specify the encoding
String s = new String(bytes, StandardCharsets.UTF_8);
InputStream is = new ByteArrayInputStream(s.getBytes());
//Create a new SOAPMessage
SOAPMessage message = MessageFactory.newInstance().createMessage(headers, is);
ByteArrayOutputStream out = new ByteArrayOutputStream();
message.writeTo(out);
out.toString();
//print the envelope and the especial characters are corrects
System.out.println(out.toString());
SOAPPart soapPart = message.getSOAPPart();
//FAIL THIS LINE
//com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory createEnvelope
//GRAVE: SAAJ0511: Unable to create envelope from given source
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
SOAPHeader soapHeader = soapEnvelope.getHeader();
SOAPBody soapBody = soapEnvelope.getBody();
Source source = soapPart.getContent();
//.... continue the code
} catch (SOAPException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
感謝
什麼是錯誤?發佈堆棧跟蹤。 –