1

I have endpoint which I am calling from SOAPUI application and everything is working, I mean I am receiving XML response which I expecting. But when I am doing it by my java app in response I am getting strange chars like

テ 0  D  E r ￱ ᅯ l ᅴ S

Here is my code which I am using:

Url url = new URL(endpoint);
connection = (HttpURLConnection) url.openConnection();
            connection.setUseCaches(false);
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Accept-Encoding", "gzip, deflate");
            connection.setRequestProperty("Accept", "*/*");
            connection.setRequestProperty("Cache-Control", "no-cache");
            connection.setRequestProperty("Authorization",
                    "Basic " + Base64.encode("username:password".getBytes()));
            connection.setRequestProperty("Content-Type", "text/xml");

BufferedInputStream response = new BufferedInputStream(connection.getInputStream());

byte[] bb = new byte[Integer.parseInt(connection.getHeaderFields().get("Content-Length").get(0))];

for (int i = 0; i < bb.length; i++) {
  bb[i] = (byte) response.read();
  System.out.printf("%s ", (char) bb[i]);
}

I found many post here which provide many ways how to read stream and I was trying: BufferedInputStream, InputStream, DataInputStream, DataByteArrayStream and much more but result always is the same. Is it possible that server sending me some wrong encoding information and data? Cause I can see in SOAPUI and POSTMAN apps that response is coming in UTF8 but I am unable to read them properly from my app.

noname
  • 565
  • 6
  • 23
  • 1
    May be this chain of streams? `new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)` – Alexey Usharovski May 31 '19 at 11:56
  • @AlexeyUsharovski I was trying that as well I am getting the same 'strange' results – noname May 31 '19 at 12:02
  • You're reading bytes(which is one byte) and are trying to get char(which is two bytes). You need to specify the encoding and construct String from the ints you read. Check this piece of code: https://stackoverflow.com/a/5713929/2266098 – NeplatnyUdaj May 31 '19 at 12:02
  • BufferedInputStream#read method return 'int' so from what you are saying I understand that if I change my 'bb' array to 'int' I should be able to read it properly cause I am reading from stream 'ints' which are returned. But it does not help and method in link which you provide does not help as well. – noname May 31 '19 at 12:16
  • 1
    No, the integers will still represent a single byte(integer is used because it can mark end of stream by returning -1). Use bufferedreader: https://stackoverflow.com/a/9856272/2266098 – NeplatnyUdaj May 31 '19 at 12:37
  • 3
    Your request agrees to accept gzip or deflate encoding (compression); check .getHeaderField(s) for 'content-encoding' to see if that's what you got. – dave_thompson_085 May 31 '19 at 13:13
  • @NeplatnyUdaj thanks a lot [stackoverflow.com/a/9856272/2266098](http://stackoverflow.com/a/9856272/2266098) helps. For some reason only that way is working thank you a lot again. – noname May 31 '19 at 13:58
  • You just got compressed data – Antoniossss May 31 '19 at 19:53

0 Answers0