Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4352956
Votes 1
Synopsis HttpURLConnection.getResponseCode() hangs forever
Category java:classes_net
Reported Against 1.3
Release Fixed
State 11-Closed, Not a Defect, bug
Priority: 4-Low
Related Bugs 4619527
Submit Date 13-JUL-2000
Description




java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

This problem occurs when sending HEAD-requests to servers that do not support
them and that just close the connection as a reaction to HEAD-requests. An
example of such a server is www.tridion.nl.

The JVM hangs when you set up a connection for a HEAD-request to www.tridion.nl,
make the connection, and ask for the response code. This only happens when using
JDK 1.3 (on both NT4 and Win2000). When using JDK 1.2.2 on Windows or Solaris,
asking for the response code results in an IOException because there isn't any
response.

The following code will show the problem:

/*------------------- start of code -------------------------*/

import java.io.*;
import java.net.*;

public class HeadConnTest {

    public static void main(String[] args) {
        try {
            System.out.println("Opening connection to: " + args[0]);
            URL url = new URL(args[0]);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("HEAD");

            System.out.println("Doing HEAD-request");
            conn.connect();

            System.out.println("Getting response code");
            int responseCode = -1;
            try {
                responseCode = conn.getResponseCode();
            }
            catch (IOException e) {
                System.err.println("Exception occurred while getting response co
de: " + e.getMessage());
            }
            System.out.println("response code = " + responseCode);

            System.out.println("disconnecting");
            conn.disconnect();
        } catch (Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace(System.err);
        }
    }
}

/*--------------------- end of code -------------------------*/

Executing the command
    java HeadConnTest http://www.tridion.nl/
with a version 1.3 JVM hangs the JVM after "Getting response code" has been
printed.
(Review ID: 107159) 
======================================================================
Work Around




Stay with jdk 1.2.
======================================================================
Evaluation
We've been unable to duplicate the behaviour observed by the
submitter. In our test case we have a http server that closes
the connection immediately. If we establish the connection
directly to the http server the client will always get a 
SocketException indicating the "Connection reset by peer" or 
"Unexpected end of file from server" (the different exception 
text arises due to timing issue on if connection is closed whilst 
the http client is still sending the http request). Note that
in the direct case we will retry the connection once we realise
that the connection is closed. However the retry just delays 
the exception a bit and no hang is observed.

If we establish the connection through a proxy server the
behaviour depends on the proxy server. With squid for example
the proxy will retry a number of times before return a 503
Service Unable error to the http client. With iPlanet/Netscape
we get an immediate 500 Error from proxy and an error page
indicating the document hasn't any data.

Based on the above I will ask the submitter to see if
further information (ie: traces) can be observed.

  xxxxx@xxxxx   2001-04-10



Further information from the submitter (via the JDC comment)
indicates that the server accepts the connection but just
doesn't respond to HEAD requests. It keeps the connection
open and doesn't close it which causes the http client to
hang waiting for a http response. In merlin we have a new
property (sun.net.client.defaultReadTimeout) which can be
specified so that http client times out after the specified
number of ms. As this is a problem with the http server I 
will close this bug.

  xxxxx@xxxxx   2001-05-29
Comments
  
  Include a link with my name & email   

Submitted On 01-MAY-2001
kampman
I've checked again, and the problem is still reproducable.
This behaviour,
however, seems to be a problem with the server used by
tridion (Microsoft
IIS/4.0). The server doesn't respond on HEAD-requests at
all. The reason
for the jvm to keep waiting forever (?) when using jdk1.3 is
that jdk1.3
sends HTTP 1.1 requests with a "Connection: Keep-Alive"
header. The
server doesn't appear to have a time-out on connections, so
it is kept
open forever. And, as long as the connection is open, the
jvm will wait
for a reponse that will never be given by this server.

So this appears to be a problem with the server and not with
Java. A
client-side timeout on the connection would be a nice thing
to have
though.


Submitted On 18-JUN-2008
NicDumZ
This is reproducable in jdk 1.5, please re open this bug :)

Test case :

import java.net.HttpURLConnection;
import java.net.URL;

public class GetResponseCodeError {
	
	public static void main(String[] args) throws Exception {
		
		HttpURLConnection urlConn;
		URL url = new URL("http://digg.com");
		
		System.out.print("Opening connection...");
		urlConn = (HttpURLConnection) url.openConnection();
		System.out.println("opened !");
	
		System.out.print("Connecting...");
		urlConn.connect();
		System.out.println("connected !");
		
		@SuppressWarnings("unused")
		int httpResponseCode = 0;
		System.out.print("Getting response code...");

		httpResponseCode = urlConn.getResponseCode(); //hangs forever
		System.out.println("done !");
	}
}

I consider this as a serious bug : http://digg.com is reachable and has a "standard HTTP header." :

curl -I http://digg.com returns :

HTTP/1.1 200 OK
Date: Wed, 18 Jun 2008 13:41:03 GMT
Server: Apache
X-Powered-By: PHP/5.2.0-8+etch1
Set-Cookie: ccc=1; expires=Thu, 18-Jun-2009 13:41:03 GMT; path=/
Set-Cookie: PHPSESSID=a76a192de6987caccd129938a46c38c5; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: private
Pragma: no-cache
Set-Cookie: 1337=0; path=/
Content-Type: text/html; charset=UTF-8


There is of course a temporary workaround to avoid that infinite wait(), consisting in setting the read timeout (HttpURLConnection.setReadTimeout(timeout) ). But I insist that this not addressing the issue since it will throw a java.net.SocketTimeoutException on a *valid* website.


Submitted On 18-JUN-2008
NicDumZ
Correcting my comment, this is reproducible in 1.6.0


Submitted On 21-JUL-2008
i noticed that digg's web server look for the browser agent  and if it is not available it does not response to request. Here is the solution for that set browser agent in HttpURLConnection  object 

public int getResponse(String webUrl) throws IOException {
		URL url = new URL(webUrl);

		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setDoOutput(true);
		connection.setConnectTimeout(5000);
		connection.setInstanceFollowRedirects(true);
		connection.setAllowUserInteraction(true);
		connection.setRequestProperty("User-Agent", "Mozilla/4.5");
		connection.connect();
		connection.disconnect();
		return connection.getResponseCode();

	}


Submitted On 21-JUL-2008
/**
 * 
 */
package com.ocricket.util;

import java.io.IOException;
import java.net.*;

/**
 * @author Dhiraj Thakur
 * @Email thakur.dheeraj@gmail.com
 * 
 */

public class InternetUtil {

	/**
	 * 
	 */
	public InternetUtil() {
		// TODO Auto-generated constructor stub
	}

	public int getResponse(String webUrl) throws IOException {
		URL url = new URL(webUrl);

		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setDoOutput(true);
		connection.setConnectTimeout(5000);
		connection.setInstanceFollowRedirects(true);
		connection.setAllowUserInteraction(true);
		connection.setRequestProperty("User-Agent", "Mozilla/4.5");
		connection.connect();
		connection.disconnect();
		return connection.getResponseCode();

	}

	public static void main(String[] argv) throws IOException {

		InternetUtil util = new InternetUtil();
		int status = util
				.getResponse("http://digg.com/odd_stuff/Dear_Charles_Manson_Little_Billy_Writes_to_Serial_Killers");
		System.out.println("status is " + status);
	}
}


Submitted On 11-FEB-2009
Got the same problem, in my case even the workaround doesnt do it.
Constellation is the request for a URL response code via a non existing proxy server (rich client application using a configured proxy that exists no more).

Without (non existing ) proxy server set, the connection timeout is honored.

Using java version "1.6.0_10"
Java(TM) SE Runtime Environment (build 1.6.0_10-b33)
Java HotSpot(TM) Client VM (build 11.0-b15, mixed mode, sharing)



PLEASE NOTE: JDK6 is formerly known as Project Mustang