响应 SOAP 请求的 UTF-8 问题



当我从 SOAP UI 发出 SOAP 请求时,它会返回正常的答案,但是当我尝试使用 Java 代码时,它会返回无法理解的字符。我试图将答案转换为 UTF8 格式,但没有帮助。请告知解决方案,可能是我的 SOAP 请求有问题。响应示例:OÄžLU, bu it must be OĞLU or MÄ°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");

试试这个:

BufferedReader

in = new BufferedReader(new InputStreamReader(con.getInputStream((, "UTF-8"((;

字符编码设置为内容类型标头的一部分。

我相信您不小心混合了字符集,这就是它无法正确显示的原因。

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

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

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

最新更新