我从Java中的Web servlet调用SAP SOAP服务。出于某种原因,SAP每次在我的请求字段中使用特殊字符(例如'è'或'à')时都会给我一个错误。 SOAP服务的WSDL以UTF-8定义,我已经相应地设置了我的字符编码,如下所示。但是我不确定这是正确的方法。另外,请注意,如果我使用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();
答案 0 :(得分:8)
由于soap-request是xml,请使用xml-header指定请求的编码:
<?xml version="1.0" encoding="UTF-8"?>
new OutputStreamWriter(con.getOutputStream())
使用平台默认编码,其中很可能是ISO8859的某种风格。请改用new OutputStreamWriter(con.getOutputStream(),"UTF-8")