2015-06-30 102 views
0

在我的Java Web服务,我实现WebServiceProvider此类并试图获得原请求的客户端已经完成。问题是我在soap消息体内的xml标签中获取了不可读的字符,如<Applicant_Place_Born>Ð&#156;оÑ&#129;ква</Applicant_Place_Born>,而不是普通的西里尔字母。所以我正在寻找如何解决这个问题的方法。可能我可以使用<Source>泛型类型而不是<SOAPMessage>,但我不知道如何将它转换为字节。
Q1:是否有可能得到的字节数组原(原始二进制数据),这样我可以手动将其解码客户端的请求?
Q2:是否有直接的方法通过为SOAP消息指定解码字符集来修复错误的字符?下面
Mojibakes在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; 
     } 
    } 
} 

回答

1

什么,你已经证明恰恰是 “Москва” 的UTF-8编码的表示。您的SOAP数据是最有可能是在具有顶部<?xml version='1.0' encoding='UTF-8' ?>这表明内容是使用UTF-8编码的XML文件。要将这些数据转换回Unicode,您需要对其进行解码。你也有一些HTML转义,所以你必须首先逃避。我用的Tcl来测试这一点:

# The original string reported 
set s "Ð&#156;оÑ&#129;ква" 
# substituting the html escapes 
set t "Ð\x9cоÑ\x81ква" 
# decode from utf-8 into Unicode 
encoding convertfrom utf-8 "Ð\x9cоÑ\x81ква" 
Москва 

所以你的SOAP信息可能是罚款,但你很可能需要处理HTML允许任何尝试从UTF-8字符串解码之前逃脱。

+0

谢谢你的回答。我明白你的解释,这就是我所问的。我需要将字符串从UTF-8解码回客户端使用的原始编码。但是我不确切知道最初使用的编码以及如何在java代码中完成编码。 – griboedov