I want to return the HTTP response exactly as-is
Don't use readLine()
then - it's as simple as that. I'd suggest using a StringWriter
instead:
StringWriter writer = new StringWriter();
char[] buffer = new char[8192];
int charsRead;
while ((charsRead = bufferedReader.read(buffer)) > 0) {
writer.write(buffer, 0, charsRead);
}
response = writer.toString();
Note that even this won't work if you get the encoding wrong. To preserve the exact HTTP response, you'd need to read (and write) it as a binary stream.