0

I'm trying to write a program that can read different types of encoding from webpage responses. Right now I'm trying to figure out how to successfully read AMF data's response. Sending it is no problem, and with my HttpWrapper, it gets the response string just fine, but many of the characters get lost in translation. For that purpose, I'm trying to receive the response as bytes, to then convert into readable text.

The big thing I'm getting is that characters get lost in translation, literally. I use a program called Charles 3.8.3 to help me get an idea of what I should be seeing in the response, both hex-wise and AMF-wise. It's generally fine when it comes to normal characters, but whenever it sees non-unicode character, I always get "ef bf bd." My code for reading the HTTP response is as follows:

BufferedReader d = new BufferedReader(new InputStreamReader(new    DataInputStream(conn.getInputStream())));
while (d.read() != -1) {
String bytes = new String(d.readLine().getBytes(), "UTF-8");
    result += bytes;
}

I then try to convert it to hex, as follows:

for (int x = 0; x < result.length(); x++) {
    byte b = (byte) result.charAt(x);
    System.out.print(String.format("%02x", b & 0xFF));
}

My output is: 0000000001000b2f312f6f6e526573756c7400046e756c6c00000**bf** Whereas Charles 3.8.3 is: 0000000001000b2f312f6f6e526573756c7400046e756c6c00000**0b**

I'm at my wits end on how to resolve this, so any help would be greatly appreciated! Thank you for your time

toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131
Eurater
  • 1
  • 2

2 Answers2

2

It looks like you're using readLine() because you're used to working with text. Wikipedia says AMF is a binary encoding, so you should be able to do something like this, rather than going through an encode/decode noop (you'd need to use ISO-8859-1, not UTF-8 for that to work) with a string.

ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];


try (InputStream in = conn.getInputStream()) {
    int read;
    while ((read = in.read(buffer)) >= 0) {
        out.write(buffer, 0, read);
    }
}

out.toByteArray();

// Convert to hex if you want.
David Ehrmann
  • 7,366
  • 2
  • 31
  • 40
  • This worked brilliantly from what I can see. After converting it to a hex string, I can see that I have the exact same hex response as Charles 3.8.3. Thank you so much :) – Eurater Jun 21 '14 at 12:51
1

Your code assumes that every stream uses UTF-8 encoding. This is simply incorrect. You will need to inspect the content-type response header field.

Julian Reschke
  • 40,156
  • 8
  • 95
  • 98
  • The response header is "application/x-amf." I didn't know there were specific ways to read each response type. How would I do that? Thank you for your reply! @Julian Reschke – Eurater Jun 21 '14 at 07:24