read() had a bug, which was causing httpclient to not correctly close
the stream. read should return -1 on no more bytes.