A
A
Adrenal1ne12016-10-13 15:49:09
Java
Adrenal1ne1, 2016-10-13 15:49:09

How to get page text and not exception?

Good afternoon.
I want to get data from the site, for this I use Apache HttpComponents.
The request is like this:

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");
    }

Exception thrown when requesting page text

15:39:20,061 ERROR [stderr] java.nio.charset.IllegalCharsetNameException: America/New_York

Response header contains erroneous data
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

Because of this title, I can't get the text of the site.
How to get around this error?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Adrenal1ne1, 2016-10-17
@Adrenal1ne1

Found the answer at _ http://stackoverflow.com/questions/309424/read-con... . just get the content through the InputStream using the getContent() method;

private String getStringFromInputStream(InputStream inputStream) {
        final int bufferSize = 1024;
        final char[] buffer = new char[bufferSize];
        final StringBuilder out = new StringBuilder();
        Reader in = null;
        try {
            in = new InputStreamReader(inputStream, "UTF-8");
            for (; ; ) {
                int rsz = in.read(buffer, 0, buffer.length);
                if (rsz < 0)
                    break;
                out.append(buffer, 0, rsz);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return out.toString();
    }

...
HttpEntity entityGet = response.getEntity();
            tempSourceFile = getStringFromInputStream(response.getEntity().getContent());
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question