首页 > 解决方案 > 响应 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");

标签: javaxmlsoaputf-8

解决方案


尝试这个:

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


推荐阅读