HTTPSampleResult err = errorResult(e, res);
err.setSampleLabel("Error: " + url.toString());
return err;
}
HttpContext localContext = new BasicHttpContext();
res.sampleStart();
final CacheManager cacheManager = getCacheManager();
if (cacheManager != null && GET.equalsIgnoreCase(method)) {
if (cacheManager.inCache(url)) {
res.sampleEnd();
res.setResponseNoContent();
res.setSuccessful(true);
return res;
}
}
try {
currentRequest = httpRequest;
// Handle the various methods
if (method.equals(POST)) {
String postBody = sendPostData((HttpPost)httpRequest);
res.setQueryString(postBody);
} else if (method.equals(PUT)) {
String putBody = sendPutData((HttpPut)httpRequest);
res.setQueryString(putBody);
}
HttpResponse httpResponse = httpClient.execute(httpRequest, localContext); // perform the sample
// Needs to be done after execute to pick up all the headers
res.setRequestHeaders(getConnectionHeaders((HttpRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST)));
Header contentType = httpResponse.getLastHeader(HEADER_CONTENT_TYPE);
if (contentType != null){
String ct = contentType.getValue();
res.setContentType(ct);
res.setEncodingAndType(ct);
}
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
res.setResponseData(readResponse(res, instream, (int) entity.getContentLength()));
}
res.sampleEnd(); // Done with the sampling proper.
currentRequest = null;
// Now collect the results into the HTTPSampleResult:
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();
res.setResponseCode(Integer.toString(statusCode));
res.setResponseMessage(statusLine.getReasonPhrase());
res.setSuccessful(isSuccessCode(statusCode));
res.setResponseHeaders(getResponseHeaders(httpResponse));
if (res.isRedirect()) {
final Header headerLocation = httpResponse.getLastHeader(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
HttpConnectionMetrics metrics = (HttpConnectionMetrics) localContext.getAttribute(CONTEXT_METRICS);
long headerBytes =
res.getResponseHeaders().length() // condensed length (without \r)
+ httpResponse.getAllHeaders().length // Add \r for each header
+ 1 // Add \r for initial header
+ 2; // final \r\n before data
long totalBytes = metrics.getReceivedBytesCount();
res.setHeadersSize((int) headerBytes);
res.setBodySize((int)(totalBytes - headerBytes));
if (log.isDebugEnabled()) {
log.debug("ResponseHeadersSize=" + res.getHeadersSize() + " Content-Length=" + res.getBodySize()
+ " Total=" + (res.getHeadersSize() + res.getBodySize()));
}
// If we redirected automatically, the URL may have changed
if (getAutoRedirects()){
HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST);
HttpHost target = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
URI redirectURI = req.getURI();
if (redirectURI.isAbsolute()){
res.setURL(redirectURI.toURL());
} else {
res.setURL(new URL(new URL(target.toURI()),redirectURI.toString()));