When I execute the following java program, sometimes I get an empty response, sometimes I get the real (redirected) content.
ByteArrayOutputStream output = new ByteArrayOutputStream();
URL url = new URL( "http://stackoverflow.com/questions/84629" );
IOUtils.copy( url.openStream(), output );
System.out.println( output.toString() );
The URL http://stackoverflow.com/questions/84629 is a redirect to http://stackoverflow.com/questions/84556/whats-your-favorite-programmer-cartoon/84629#84629.
I looked through other SO questions and tried to use the suggested HttpUrlConnection
, but the result was the same. The response code is always 200, but sometimes there is the correct html output, sometimes it's just an empty string.
Can you explain what is happening here?
EDIT
Here's the code without Apache commons:
ByteArrayOutputStream output = new ByteArrayOutputStream();
URL url = new URI( "http://stackoverflow.com/questions/84629" ).toURL();
InputStream openStream = url.openStream();
byte[] buffer = new byte[ 1024 ];
int size = 0;
while( (size = openStream.read( buffer ) ) != -1 ) {
output.write( buffer, 0, size );
}
System.out.println( output.toString() );
I'm using Windows XP and Java 1.6.0_17.
I captured the traffic using wireshark:
GET /questions/84629 HTTP/1.1 User-Agent: Java/1.6.0_17 Host: stackoverflow.com Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 Connection: keep-alive HTTP/1.1 200 OK Cache-Control: private Server: Microsoft-IIS/7.0 Date: Tue, 10 Nov 2009 22:42:42 GMT Content-Length: 0