manually parse HTTP headers to work around some bug in HttpURLConnection