响应SOAP请求的UTF-8问题

时间:2019-07-24 08:26:16

标签: java xml soap utf-8

当我从SOAP UI发出SOAP请求时,它返回正常答案,但是当我从Java代码尝试时,它返回无法理解的字符。我试图将答案转换为UTF8格式,但没有帮助。请提出解决方案,我的SOAP请求可能有问题。响应示例:OÄžLU,但必须为OĞLUMÄ°KAYIL ,而不是MİKAYIL

            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            String userCredentials = username + ":" + password;
            String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));
            con.setRequestProperty("Authorization", basicAuth);
            con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type", "text/xml");    
            con.setDoOutput(true);   
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(myXML);
            wr.flush();
            wr.close();

            String responseStatus = con.getResponseMessage();
            System.out.println(responseStatus);

            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            String xmlResponse = response.toString();

我尝试过:

  ByteBuffer buffer = ByteBuffer.wrap(xmlResponse.getBytes("UTF-8"));
  String converted = new String(buffer.array(), "UTF-8");

3 个答案:

答案 0 :(得分:1)

尝试一下:

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(),“ UTF-8”));

答案 1 :(得分:0)

您会尝试吗?
con.setRequestProperty("Content-Type", "text/xml; charset=utf-8");

答案 2 :(得分:0)

字符编码被设置为Content-Type标头的一部分。

我相信您不小心混用了字符集,这就是为什么它不能正确显示的原因。

尝试将字符集添加到Content-Type中,如下所示:

con.setRequestProperty("Content-Type", "text/xml; charset=utf-8");