[英]Send SOAP Request with XML Parameter UTF-8 Java
I am looking for an advice here with my problem. 我在这里寻找有关我的问题的建议。 I have a SOAP Request in Java with the following code: 我有一个带有以下代码的Java SOAP请求:
private static SOAPMessage createSOAPRequest(String shellType, String filterCondition,
Map<String, String> AndySiteToSync) throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage soapMessage = messageFactory.createMessage();
createSoapEnvelope(soapMessage, shellType, filterCondition, AndySiteToSync);
MimeHeaders headers = soapMessage.getMimeHeaders();
soapMessage.saveChanges();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
soapMessage.writeTo(stream);
return soapMessage;
}
I have the following structure on the XML. 我在XML上具有以下结构。
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:gen="http://general.service.webservices.skire.com">
<soap:Header/>
<soap:Body>
<gen:updateShell>
<!--Optional:-->
<gen:shortname>xxx</gen:shortname>
<!--Optional:-->
<gen:authcode>xxx</gen:authcode>
<!--Optional:-->
<gen:shellType>xxx</gen:shellType>
<!--Optional:-->
<gen:shellXML>
<![CDATA[
<List_Wrapper>
<_shell>
<paraminside>letterwithutf8_ññ</uuu_longitude>
</_shell>
</List_Wrapper>
]]>
</gen:shellXML>
</gen:updateShell>
</soap:Body>
</soap:Envelope>
It is working very good with characters without ñ or áéíóú, but when I pass a parameter with ñ for example I see on the result ñ. 对于没有ñ或áéíóú的字符,它的效果非常好,但是例如当我通过ñ传递参数时,我会在结果±上看到。 When I print the input xml it is showing the XML fine with the correct characters but when I send the request the result is showing the incorrect character (ñ) 当我打印输入xml时,它会正确显示具有正确字符的XML,但是当我发送请求时,结果将显示不正确的字符(ñ)
I paste the same input XML in the SOAPUI and the result was good, the ñ characters was introduce fine. 我将相同的输入XML粘贴到SOAPUI中,结果很好,ñ字符被很好地引入了。 The problem is when I send the request using Java. 问题是当我使用Java发送请求时。 I assumed it is the encoding of the message. 我假设这是消息的编码。 So I added these lines before saveChanges: 所以我在saveChanges之前添加了以下几行:
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("Content-Type", "application/soap+xml;charset=UTF-8");
Alse test with: headers.setHeader("Content-Type", "application/soap+xml;charset=UTF-8"); 还可以通过以下方式进行测试:headers.setHeader(“ Content-Type”,“ application / soap + xml; charset = UTF-8”);
These headers are the same as the SOAPUI, with the exception of the "action" I made sure the headers were ok printing it. 这些标头与SOAPUI相同,除了“操作”外,我确保标头可以正常打印。
for (int iii = 0; iii < headers.getHeader("Content-Type").length; iii++) {
System.out.println((headers.getHeader("Content-Type"))[iii]);
}
and I got the response application/soap+xml;charset=UTF-8 并且我得到了响应应用程序/ soap + xml; charset = UTF-8
This way I am creating the connection: 这样,我可以创建连接:
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(....))
SOAPUI SOAPUI
POST https://xxxxxxxxxxxxxxxxx/WebServices HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/soap+xml;charset=UTF-8;action="urn:xxxxxxx"
Content-Length: 4381
Host: xxxxxxxxxxxxxxxx
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
Thanks. 谢谢。
=========================================== ==========================================
Thanks to Andreas I did some more testing and the encoding problem was before the SOAP Request, I got the information from a URL in json format with the following code: 感谢Andreas,我进行了更多测试,并且编码问题出现在SOAP Request之前,我使用以下代码从json格式的URL中获取了信息:
JSONArray jsonDataFinal = new JSONArray();
JSONArray jsonData = (JSONArray) parser.parse(readUrl(URL));
And then: 接着:
List<Map<String,String>> mergeArray = new ArrayList<Map<String,String>>();
Map<String, String> mergeObj = null;
for (Object o : sitesArray)
{
if((siteAndy.get("xxxx")) == null) mergeObj.put("xxxx", "");
else mergeObj.put("xxxx", ((String) siteAndy.get("xxxx")).trim());
}
At what point should I use the utf-8 encoding? 在什么时候应该使用utf-8编码?
Thanks 谢谢
I change to this: 我改为:
reader = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
And my problem was fix. 我的问题是解决。
Thanks!! 谢谢!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.