Answer the question
In order to leave comments, you need to log in
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");
}
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
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question