在我的java web服务上,我实现了WebServiceProvider,并试图获得客户机已经完成的原始请求。问题是,我在soap消息体的xml标记中得到了诸如<Applicant_Place_Born>Москва</Applicant_Place_Born>这样的不可读字符,而不是普通的西里尔字母。所以我在寻找解决这个问题的方法。也许我可以使用<Source>泛型类型而不是<SOAPMessage>,但我不知道如何将它转换为字节。
Q1:是否可以将客户端的请求作为原始字节数组(原始二进制数据)来获得,以便我可以手动解码它?
Q2:是否有通过为SOAP消息指定解码字符集来修复错误字符的直接方法?
我的当前代码如下:
@WebServiceProvider(
portName="SoaprequestImplPort",
serviceName="services/soaprequest",
targetNamespace="http://tempuri.org/soaprequest",
wsdlLocation="/wsdl/SoaprequestImpl.wsdl"
)
@BindingType(value="http://schemas.xmlsoap.org/wsdl/soap/http")
@ServiceMode(value=javax.xml.ws.Service.Mode.MESSAGE)
public class SoaprequestImpl implements Provider<SOAPMessage> {
private static final String hResponse = "<soapenv:Envelope xmlns:soapenv=\\";
public SOAPMessage invoke(SOAPMessage req) {
getSOAPMessage(req);
SOAPMessage res = null;
try {
res = makeSOAPMessage(hResponse);
} catch (Exception e) {
System.out.println("Exception: occurred " + e);
}
return res;
}
private String getSOAPMessage(SOAPMessage msg) {
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
msg.writeTo(baos);
OutputStream outputStream = new FileOutputStream ("/opt/data/tomcat/end.txt");
baos.writeTo(outputStream);
} catch(Exception e) {
e.printStackTrace();
}
return s;
}
private SOAPMessage makeSOAPMessage(String msg) {
try {
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
message.getSOAPPart().setContent((Source)new StreamSource(new StringReader(msg)));
message.saveChanges();
return message;
} catch (Exception e) {
return null;
}
}
}发布于 2015-06-30 10:00:07
您所展示的只是"Москва“的UTF-8编码表示。您的SOAP数据很可能位于顶部有<?xml version='1.0' encoding='UTF-8' ?>的XML文件中,这表明内容是使用UTF-8编码的。要将这些数据转换回Unicode,您需要对其进行解码。您也有一些HTML转义在那里,所以您必须先取消转义。我使用Tcl来测试这个:
# The original string reported
set s "Москва"
# substituting the html escapes
set t "Ð\x9cоÑ\x81ква"
# decode from utf-8 into Unicode
encoding convertfrom utf-8 "Ð\x9cоÑ\x81ква"
Москва所以您的SOAP信息可能很好,但您很可能需要处理HTML转义,然后才允许任何尝试从utf-8解码字符串。
https://stackoverflow.com/questions/31134506
复制相似问题