I've had the same error as you have and it turned out that there was nothing wrong with the code. The problem was that the webserver was sending the wrong Content-Type header.
Try wireshark or something similar to see what content-type the webserver is sending.