At the moment HttpURLConnection.getInputStream() returns
a (real) input stream even if there is no entity body
in the response ( Content-Length field in the header is zero).
If you try to read from this stream the application will block
until the server closes the socket.
---- Test case ----
import java.net.*;
import java.io.*;
public class Test {
public static void main (String args[]) {
try {
URL url = new URL ("http://sunweb.ireland/");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection ();
urlc.setRequestMethod ("OPTIONS");
int r = urlc.getResponseCode ();
System.out.println (r);
InputStream is = urlc.getInputStream ();
while ((r=is.read()) != -1) {
System.out.write (r);
}
} catch (Exception e) {
e.printStackTrace ();
}
}
}
a (real) input stream even if there is no entity body
in the response ( Content-Length field in the header is zero).
If you try to read from this stream the application will block
until the server closes the socket.
---- Test case ----
import java.net.*;
import java.io.*;
public class Test {
public static void main (String args[]) {
try {
URL url = new URL ("http://sunweb.ireland/");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection ();
urlc.setRequestMethod ("OPTIONS");
int r = urlc.getResponseCode ();
System.out.println (r);
InputStream is = urlc.getInputStream ();
while ((r=is.read()) != -1) {
System.out.write (r);
}
} catch (Exception e) {
e.printStackTrace ();
}
}
}
- duplicates
-
JDK-4507412 HttpURLConnection Keep-Alive fails for requests that return content-length 0
-
- Closed
-