SOAP 消息中的 Mojibakes
Mojibakes in SOAP message
在我的 java 网络服务上,我实现了 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;
}
}
}
您所显示的只是“Москва”的 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 信息可能没问题,但您很可能需要在允许任何尝试从 utf-8 解码字符串之前处理 HTML 转义。
在我的 java 网络服务上,我实现了 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;
}
}
}
您所显示的只是“Москва”的 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 信息可能没问题,但您很可能需要在允许任何尝试从 utf-8 解码字符串之前处理 HTML 转义。