I have some automated tests that use HttpURLConnection to exercise a RESTful API.
Part of my code (below) asserts that the the response returns a certain HTTP Response code. I am expecting a HTTP 206 Response, but getResponseCode is always returning 200. However if I hit the url directly using curl, I get 'HTTP/1.1 206 Partial Content' as expected.
URL requestURL = new URL(url);
HttpURLConnection connection = (HttpURLConnection) requestURL.openConnection();
try {
connection.setRequestProperty("Connection", "close");
connection.setReadTimeout(5000);
assertEquals("Request successfully handled",
expectedResponseCode,
connection.getResponseCode());
InputStream input = connection.getInputStream();
try {
return toString(input);
} finally {
input.close();
}
} finally {
connection.disconnect();
}
Any ideas on why this is happening and how to get the behaviour I want?