String location, Cookie[] cookies, boolean post)
throws IOException {
byte[] byteArray = null;
HttpMethod method = null;
try {
HttpClient client =
new HttpClient(new SimpleHttpConnectionManager());
if (location == null) {
return byteArray;
}
else if (!location.startsWith(HTTP_WITH_SLASH) &&
!location.startsWith(HTTPS_WITH_SLASH)) {
location = HTTP_WITH_SLASH + location;
}
HostConfiguration hostConfig = new HostConfiguration();
hostConfig.setHost(new URI(location));
if (Validator.isNotNull(PROXY_HOST) && PROXY_PORT > 0) {
hostConfig.setProxy(PROXY_HOST, PROXY_PORT);
}
client.setHostConfiguration(hostConfig);
client.setConnectionTimeout(5000);
client.setTimeout(5000);
if (cookies != null && cookies.length > 0) {
HttpState state = new HttpState();
state.addCookies(cookies);
state.setCookiePolicy(CookiePolicy.COMPATIBILITY);
client.setState(state);
}
if (post) {
method = new PostMethod(location);
}
else {
method = new GetMethod(location);
}
method.setFollowRedirects(true);
client.executeMethod(method);
Header locationHeader = method.getResponseHeader("location");
if (locationHeader != null) {
return URLtoByteArray(locationHeader.getValue(), cookies, post);
}
InputStream is = method.getResponseBodyAsStream();
if (is != null) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] bytes = new byte[512];
for (int i = is.read(bytes, 0, 512); i != -1;
i = is.read(bytes, 0, 512)) {
buffer.write(bytes, 0, i);
}
byteArray = buffer.toByteArray();
is.close();
buffer.close();
}
return byteArray;
}
finally {
try {
if (method != null) {
method.releaseConnection();
}
}
catch (Exception e) {
Logger.error(Http.class,e.getMessage(),e);
}