如何在SOAP请求中设置字符编码
我正在从Java中的Web servlet调用SAP
SOAP服务。由于某种原因,每次我在请求的字段中使用特殊字符(例如“è”或“à”)时,SAP都会给我一个错误。SOAP
Service的WSDL在UTF-8中定义,我已经相应地设置了编码" title="字符编码">字符编码,如下所示。但是我不确定这是正确的方法。另外,请注意,如果我使用SOAP
UI(具有相同的信封),则请求可以正常工作,因此它一定是Java方面的东西。
URL url = new URL(SOAP_URL);String authorization = Base64Coder.encodeString(SOAP_USERNAME + ":" + SOAP_PASSWORD);
String envelope = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:urn='urn:sap-com:document:sap:soap:functions:mc-style'><soapenv:Header/><soapenv:Body><urn:ZwsMaintainTkt><item>à</item></urn:ZwsMaintainTkt></soapenv:Body></soapenv:Envelope>";
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(SOAP_TIMEOUT);
con.setRequestMethod("POST");
con.setRequestProperty("Content-type", "text/xml; charset=utf-8");
con.setRequestProperty("SOAPAction", SOAP_ACTION_ZWSMANTAINTKT);
con.setRequestProperty("Authorization", "Basic " + authorization);
con.setDoOutput(true);
con.setDoInput(true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(con.getOutputStream());
outputStreamWriter.write(envelope);
outputStreamWriter.close();
InputStream inputStream = con.getInputStream();
回答:
- 由于soap-request是xml,因此请使用xml-header指定请求的编码:
<?xml version="1.0" encoding="UTF-8"?>
new OutputStreamWriter(con.getOutputStream())
使用平台默认编码,这很有可能是ISO8859的某种形式。使用new OutputStreamWriter(con.getOutputStream(),"UTF-8")
替代
以上是 如何在SOAP请求中设置字符编码 的全部内容, 来源链接: utcz.com/qa/419831.html