Good day. I want to get data from the site, for this I use Apache HttpComponents. Request such:

public Header[] sendRequest() throws IOException { CloseableHttpResponse responsePost = null; HttpPost httpPost; httpPost = new HttpPost(urlForRequest); httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8")); responsePost = httpClient.execute(httpPost); HttpEntity entityPost = responsePost.getEntity(); sourceTextFile = EntityUtils.toString(entityPost); responsePost.close(); return responsePost.getHeaders("Location"); } 

At request of the text of the page the exception takes off

 15:39:20,061 ERROR [stderr] java.nio.charset.IllegalCharsetNameException: America/New_York Заголовок ответа содержит ошибочные данные Connection : close Content-Encoding: gzip Content-Length: 20 Content-Type: text/html; charset=America/New_York Date : Thu, 13 Oct 2016 12:41:28 GMT 

Content-Type given by the site is text / html; charset = America / New_York. Because of this on line

 sourceTextFile = EntityUtils.toString(entityPost); 

getting exception.

How do I get the wrong ContentType header to read the contents of a page to a file?

    1 answer 1

    The error occurs because the EntityUtils.toString() method when reading an entity uses the encoding from the http response header and throws java.nio.charset.IllegalCharsetNameException , since America/New_York not a valid encoding (and generally encoding).
    To work around this error, you can treat the response body as a stream using entityPost.getContent() .

    • Thank you, I did. All options for getting text via getContent () found here http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string - Adrenal1ne