Samir Amanov
Samir Amanov

Reputation: 169

UTF-8 problem in response of SOAP request

When I make the SOAP request from SOAP UI it returns normal answer, but when I try from Java code it returns not understandable characters. I tried to convert answer to UTF8 format, but it did not help. Please advise a solution, may be something wrong with my SOAP request. Example of response: OÄžLU, bu it must be OĞLU or MÄ°KAYIL instead of 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();

I tried:

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

Upvotes: 0

Views: 6860

Answers (3)

yeahseol
yeahseol

Reputation: 385

Would you try this?
con.setRequestProperty("Content-Type", "text/xml; charset=utf-8");

Upvotes: 0

Janos Vinceller
Janos Vinceller

Reputation: 1266

Try this:

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

Upvotes: 2

Daniel Scarfe
Daniel Scarfe

Reputation: 219

The character encoding is set as part of the Content-Type header.

I believe you're accidentally mixing charsets, which is why it is not displaying properly.

Try adding the charset to Content-Type like so:

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

Upvotes: 0

Related Questions