InputStream instream = httpMethod.getResponseBodyAsStream();
if (instream != null) {// will be null for HEAD
instream = new CountingInputStream(instream);
try {
Header responseHeader = httpMethod.getResponseHeader(HEADER_CONTENT_ENCODING);
if (responseHeader!= null && ENCODING_GZIP.equals(responseHeader.getValue())) {
InputStream tmpInput = new GZIPInputStream(instream); // tmp inputstream needs to have a good counting
res.setResponseData(readResponse(res, tmpInput, (int) httpMethod.getResponseContentLength()));
} else {
res.setResponseData(readResponse(res, instream, (int) httpMethod.getResponseContentLength()));
}
} finally {
JOrphanUtils.closeQuietly(instream);
}
}
res.sampleEnd();
// Done with the sampling proper.
// Now collect the results into the HTTPSampleResult:
res.setSampleLabel(httpMethod.getURI().toString());
// Pick up Actual path (after redirects)
res.setResponseCode(Integer.toString(statusCode));
res.setSuccessful(isSuccessCode(statusCode));
res.setResponseMessage(httpMethod.getStatusText());
String ct = null;
Header h = httpMethod.getResponseHeader(HEADER_CONTENT_TYPE);
if (h != null)// Can be missing, e.g. on redirect
{
ct = h.getValue();
res.setContentType(ct);// e.g. text/html; charset=ISO-8859-1
res.setEncodingAndType(ct);
}
res.setResponseHeaders(getResponseHeaders(httpMethod));
if (res.isRedirect()) {
final Header headerLocation = httpMethod.getResponseHeader(HEADER_LOCATION);
if (headerLocation == null) { // HTTP protocol violation, but avoids NPE
throw new IllegalArgumentException("Missing location header");
}
res.setRedirectLocation(headerLocation.getValue());
}
// record some sizes to allow HTTPSampleResult.getBytes() with different options
if (instream != null) {
res.setBodySize(((CountingInputStream) instream).getCount());