You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by "bchevillon.ml@tiscali.fr" <bc...@tiscali.fr> on 2005/06/15 10:56:21 UTC
Abort HttpMethod that is hanging due to bad proxy config
Hello,
I use HttpClient in my application to download description
file. I work on ensuring that if the application gets
closed, everything stop as fast as possible.
I'm getting problem with this: as many companies network,
http request get out through an http proxy. If I configure
correctly the proxy settings, and if I try to abort a
GetMethod, the connection is immediately stopped, no problem.
Now if I don't configure the proxy settings, the abort
method does not work, and my application hangs until timeout.
As my application will be deployed on many networks on which
I cannot be sure of the correct settings, I cannot accept
that the application hangs until timeout.
For better understanding, here is the unit case I'm using to
test it (source code at the end of this email):
There is two thread:
- The main thread that starts the second one, sleeps for 3
seconds then aborts the second one.
- The GetMethod thread that execute the GetMethod on a dummy
site (http://test.dyndns.org does not work for me) that do
not reply and timeouts in 10 seconds.
Preliminary for testing:
You must be on a network with a proxy. Configure the proxy
settings at the beginning of the class.
What do the test:
It just gets http://test.dyndns.org
Expected result:
- http://test.dyndns.org does not reply correctly
- so the execute wait for 10 seconds,
- and the main method abort the GetMethod after only 3 seconds
- So the application stops just after the abort and don't
wait for 10 seconds.
1st test : with proxy settings.
- uncomment the line "
client.getHostConfiguration().setProxy(PROXY_HOST, PROXY_PORT);"
- Compile and start the class
Result:
- The application stops correctly in 3 seconds (just after
the abort).
2nd test : without proxy settings.
- comment the line "
client.getHostConfiguration().setProxy(PROXY_HOST, PROXY_PORT);"
- Compile and start the class
Result:
- the abort method is executed in 3 seconds
- BUT the execute method still hangs until the end of the 10
seconds timeout.
Do anybody can produce the same type of acting?
See below my test class and logs.
Thanks in advance
Benjamin Chevillon
France Télécom R&D
---------------------------------------------------------------
File MethodAbortTest.java
import java.io.IOException;
import java.util.Date;
import
org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class MethodAbortTest extends Thread {
public static final String PROXY_HOST = "proxy";
public static final int PROXY_PORT = 8080;
// An url that do not reply
public static final String GETURL = "http://test.dyndns.org/";
HttpClient client;
private GetMethod get;
public static void main(String[] args) {
System.setProperty("org.apache.commons.logging.Log",
"org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime",
"true");
System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire.header",
"debug");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient",
"debug");
MethodAbortTest myTest = new MethodAbortTest();
myTest.start();
System.out.println("done");
try {
Thread.sleep(3000);
} catch (InterruptedException exc) {
exc.printStackTrace();
}
myTest.cancel();
}
public void run() {
HttpClientParams params = new HttpClientParams();
params.setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(0, true));
client = new HttpClient(params);
client.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
// Comment next line to unconfigure proxy settings
client.getHostConfiguration().setProxy(PROXY_HOST,
PROXY_PORT);
get = new GetMethod(GETURL);
try {
System.out.println(new Date() + " execute");
int statuscode = client.executeMethod(get);
System.out.println(statuscode);
System.out.println("execution done");
// print response to stdout
System.out.println(get.getResponseBodyAsString());
} catch (HttpException exc) {
// TODO Auto-generated catch block
exc.printStackTrace();
} catch (IOException exc) {
// TODO Auto-generated catch block
System.out.println("IO");
exc.printStackTrace();
} finally {
// be sure the connection is released back to the
connection
// manager
System.out.println("End Of HttpGet");
get.releaseConnection();
}
}
public void cancel() {
// this.interrupt();
System.out.println(new Date() + " canceling");
get.abort();
System.out.println("aborted?" + get.isAborted());
}
}
---------------------------------------------------
With correct proxy setting, the class aborts immediately
done
2005/06/15 10:50:56:988 CEST [DEBUG] DefaultHttpParams - Set
parameter http.useragent = Jakarta Commons-HttpClient/3.0-rc2
2005/06/15 10:50:56:988 CEST [DEBUG] DefaultHttpParams - Set
parameter http.protocol.version = HTTP/1.1
2005/06/15 10:50:56:988 CEST [DEBUG] DefaultHttpParams - Set
parameter http.connection-manager.class = class
org.apache.commons.httpclient.SimpleHttpConnectionManager
2005/06/15 10:50:56:988 CEST [DEBUG] DefaultHttpParams - Set
parameter http.protocol.cookie-policy = rfc2109
2005/06/15 10:50:57:003 CEST [DEBUG] DefaultHttpParams - Set
parameter http.protocol.element-charset = US-ASCII
2005/06/15 10:50:57:003 CEST [DEBUG] DefaultHttpParams - Set
parameter http.protocol.content-charset = ISO-8859-1
2005/06/15 10:50:57:003 CEST [DEBUG] DefaultHttpParams - Set
parameter http.method.retry-handler =
org.apache.commons.httpclient.DefaultHttpMethodRetryHandler@167d940
2005/06/15 10:50:57:003 CEST [DEBUG] DefaultHttpParams - Set
parameter http.dateparser.patterns = [EEE, dd MMM yyyy
HH:mm:ss zzz, EEEE, dd-MMM-yy HH:mm:ss zzz, EEE MMM d
HH:mm:ss yyyy, EEE, dd-MMM-yyyy HH:mm:ss z, EEE, dd-MMM-yyyy
HH-mm-ss z, EEE, dd MMM yy HH:mm:ss z, EEE dd-MMM-yyyy
HH:mm:ss z, EEE dd MMM yyyy HH:mm:ss z, EEE dd-MMM-yyyy
HH-mm-ss z, EEE dd-MMM-yy HH:mm:ss z, EEE dd MMM yy HH:mm:ss
z, EEE,dd-MMM-yy HH:mm:ss z, EEE,dd-MMM-yyyy HH:mm:ss z,
EEE, dd-MM-yyyy HH:mm:ss z]
2005/06/15 10:50:57:003 CEST [DEBUG] DefaultHttpParams - Set
parameter http.method.retry-handler =
org.apache.commons.httpclient.DefaultHttpMethodRetryHandler@fd13b5
2005/06/15 10:50:57:019 CEST [DEBUG] HttpClient - Java
version: 1.4.2_07
2005/06/15 10:50:57:019 CEST [DEBUG] HttpClient - Java
vendor: Sun Microsystems Inc.
2005/06/15 10:50:57:019 CEST [DEBUG] HttpClient - Java class
path:
C:\workspace\Temp\Dev\classes;C:\workspace\Temp\Dev\lib\commons-codec-1.3.jar;C:\workspace\Temp\Dev\lib\commons-httpclient-3.0-rc2.jar;C:\workspace\Temp\Dev\lib\commons-logging.jar
2005/06/15 10:50:57:019 CEST [DEBUG] HttpClient - Operating
system name: Windows XP
2005/06/15 10:50:57:019 CEST [DEBUG] HttpClient - Operating
system architecture: x86
2005/06/15 10:50:57:019 CEST [DEBUG] HttpClient - Operating
system version: 5.1
2005/06/15 10:50:57:191 CEST [DEBUG] HttpClient - SUN 1.42:
SUN (DSA key/parameter generation; DSA signing; SHA-1, MD5
digests; SecureRandom; X.509 certificates; JKS keystore;
PKIX CertPathValidator; PKIX CertPathBuilder; LDAP,
Collection CertStores)
2005/06/15 10:50:57:191 CEST [DEBUG] HttpClient - SunJSSE
1.42: Sun JSSE provider(implements RSA Signatures, PKCS12,
SunX509 key/trust factories, SSLv3, TLSv1)
2005/06/15 10:50:57:191 CEST [DEBUG] HttpClient - SunRsaSign
1.42: SUN's provider for RSA signatures
2005/06/15 10:50:57:191 CEST [DEBUG] HttpClient - SunJCE
1.42: SunJCE Provider (implements DES, Triple DES, AES,
Blowfish, PBE, Diffie-Hellman, HMAC-MD5, HMAC-SHA1)
2005/06/15 10:50:57:191 CEST [DEBUG] HttpClient - SunJGSS
1.0: Sun (Kerberos v5)
2005/06/15 10:50:57:207 CEST [DEBUG] DefaultHttpParams - Set
parameter http.connection.timeout = 10000
Wed Jun 15 10:50:57 CEST 2005 execute
2005/06/15 10:50:57:394 CEST [DEBUG] HttpConnection - Open
connection to proxy:8080
2005/06/15 10:50:57:425 CEST [DEBUG] header - >> "GET
http://test.dyndns.org/ HTTP/1.1[\r][\n]"
2005/06/15 10:50:57:441 CEST [DEBUG] HttpMethodBase - Adding
Host request header
2005/06/15 10:50:57:457 CEST [DEBUG] header - >>
"User-Agent: Jakarta Commons-HttpClient/3.0-rc2[\r][\n]"
2005/06/15 10:50:57:457 CEST [DEBUG] header - >> "Host:
test.dyndns.org[\r][\n]"
2005/06/15 10:50:57:457 CEST [DEBUG] header - >>
"Proxy-Connection: Keep-Alive[\r][\n]"
2005/06/15 10:50:57:457 CEST [DEBUG] header - >> "[\r][\n]"
Wed Jun 15 10:50:59 CEST 2005 canceling
2005/06/15 10:50:59:878 CEST [DEBUG] HttpMethodDirector -
Closing the connection.
2005/06/15 10:50:59:878 CEST [DEBUG] HttpMethodDirector -
Method retry handler returned false. Automatic recovery will
not be attempted
2005/06/15 10:50:59:878 CEST [DEBUG] HttpConnection -
Releasing connection back to connection manager.
IO
java.net.SocketException: socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at
java.io.BufferedInputStream.fill(BufferedInputStream.java:183)
at
java.io.BufferedInputStream.read(BufferedInputStream.java:201)
at
org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:77)
at
org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:105)
at
org.apache.commons.httpclient.HttpConnection.readLine(HttpConnection.java:1110)
at
org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1824)
at
org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1584)
at
org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:995)
at
org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:393)
End Of HttpGet at
org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:168)
at
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:393)
at
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
at MethodAbortTest.run(MethodAbortTest.java:86)
2005/06/15 10:50:59:878 CEST [DEBUG] HttpConnection -
Releasing connection back to connection manager.
aborted?true
---------------------------------------------------
Without proxy, class hangs for 10 seconds
done
2005/06/15 10:54:02:661 CEST [DEBUG] DefaultHttpParams - Set
parameter http.useragent = Jakarta Commons-HttpClient/3.0-rc2
2005/06/15 10:54:02:661 CEST [DEBUG] DefaultHttpParams - Set
parameter http.protocol.version = HTTP/1.1
2005/06/15 10:54:02:676 CEST [DEBUG] DefaultHttpParams - Set
parameter http.connection-manager.class = class
org.apache.commons.httpclient.SimpleHttpConnectionManager
2005/06/15 10:54:02:676 CEST [DEBUG] DefaultHttpParams - Set
parameter http.protocol.cookie-policy = rfc2109
2005/06/15 10:54:02:676 CEST [DEBUG] DefaultHttpParams - Set
parameter http.protocol.element-charset = US-ASCII
2005/06/15 10:54:02:676 CEST [DEBUG] DefaultHttpParams - Set
parameter http.protocol.content-charset = ISO-8859-1
2005/06/15 10:54:02:692 CEST [DEBUG] DefaultHttpParams - Set
parameter http.method.retry-handler =
org.apache.commons.httpclient.DefaultHttpMethodRetryHandler@167d940
2005/06/15 10:54:02:692 CEST [DEBUG] DefaultHttpParams - Set
parameter http.dateparser.patterns = [EEE, dd MMM yyyy
HH:mm:ss zzz, EEEE, dd-MMM-yy HH:mm:ss zzz, EEE MMM d
HH:mm:ss yyyy, EEE, dd-MMM-yyyy HH:mm:ss z, EEE, dd-MMM-yyyy
HH-mm-ss z, EEE, dd MMM yy HH:mm:ss z, EEE dd-MMM-yyyy
HH:mm:ss z, EEE dd MMM yyyy HH:mm:ss z, EEE dd-MMM-yyyy
HH-mm-ss z, EEE dd-MMM-yy HH:mm:ss z, EEE dd MMM yy HH:mm:ss
z, EEE,dd-MMM-yy HH:mm:ss z, EEE,dd-MMM-yyyy HH:mm:ss z,
EEE, dd-MM-yyyy HH:mm:ss z]
2005/06/15 10:54:02:692 CEST [DEBUG] DefaultHttpParams - Set
parameter http.method.retry-handler =
org.apache.commons.httpclient.DefaultHttpMethodRetryHandler@fd13b5
2005/06/15 10:54:02:801 CEST [DEBUG] HttpClient - Java
version: 1.4.2_07
2005/06/15 10:54:02:801 CEST [DEBUG] HttpClient - Java
vendor: Sun Microsystems Inc.
2005/06/15 10:50:57:019 CEST [DEBUG] HttpClient - Java class
path:
C:\workspace\Temp\Dev\classes;C:\workspace\Temp\Dev\lib\commons-codec-1.3.jar;C:\workspace\Temp\Dev\lib\commons-httpclient-3.0-rc2.jar;C:\workspace\Temp\Dev\lib\commons-logging.jar
2005/06/15 10:54:02:801 CEST [DEBUG] HttpClient - Operating
system name: Windows XP
2005/06/15 10:54:02:801 CEST [DEBUG] HttpClient - Operating
system architecture: x86
2005/06/15 10:54:02:801 CEST [DEBUG] HttpClient - Operating
system version: 5.1
2005/06/15 10:54:02:973 CEST [DEBUG] HttpClient - SUN 1.42:
SUN (DSA key/parameter generation; DSA signing; SHA-1, MD5
digests; SecureRandom; X.509 certificates; JKS keystore;
PKIX CertPathValidator; PKIX CertPathBuilder; LDAP,
Collection CertStores)
2005/06/15 10:54:02:973 CEST [DEBUG] HttpClient - SunJSSE
1.42: Sun JSSE provider(implements RSA Signatures, PKCS12,
SunX509 key/trust factories, SSLv3, TLSv1)
2005/06/15 10:54:02:973 CEST [DEBUG] HttpClient - SunRsaSign
1.42: SUN's provider for RSA signatures
2005/06/15 10:54:02:973 CEST [DEBUG] HttpClient - SunJCE
1.42: SunJCE Provider (implements DES, Triple DES, AES,
Blowfish, PBE, Diffie-Hellman, HMAC-MD5, HMAC-SHA1)
2005/06/15 10:54:02:973 CEST [DEBUG] HttpClient - SunJGSS
1.0: Sun (Kerberos v5)
2005/06/15 10:54:02:989 CEST [DEBUG] DefaultHttpParams - Set
parameter http.connection.timeout = 10000
Wed Jun 15 10:54:03 CEST 2005 execute
2005/06/15 10:54:03:098 CEST [DEBUG] HttpConnection - Open
connection to test.dyndns.org:80
Wed Jun 15 10:54:05 CEST 2005 canceling
aborted?true
IO
2005/06/15 10:54:13:270 CEST [DEBUG] HttpMethodDirector -
Closing the connection.
2005/06/15 10:54:13:270 CEST [DEBUG] HttpMethodDirector -
Method retry handler returned false. Automatic recovery will
not be attempted
2005/06/15 10:54:13:270 CEST [DEBUG] HttpConnection -
Releasing connection back to connection manager.
org.apache.commons.httpclient.ConnectTimeoutException: The
host did not accept the connection within timeout of 10000 ms
at
org.apache.commons.httpclient.protocol.ReflectionSocketFactory.createSocket(ReflectionSocketFactory.java:154)
at
org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:124)
at
org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:704)
at
org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:382)
at
org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:168)
at
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:393)
at
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
at MethodAbortTest.run(MethodAbortTest.java:86)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:305)
at
java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:171)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:158)
at java.net.Socket.connect(Socket.java:452)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)End
Of HttpGet
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at
org.apache.commons.httpclient.protocol.ReflectionSocketFactory.createSocket(ReflectionSocketFactory.java:139)
... 7 more
************************ ADSL JUSQU'A 8 MEGA + 3 MOIS DE TELEPHONE GRATUIT ************************
L'ultra haut débit à 15EUR/mois seulement ! Et vous téléphonez gratuitement en France vers les postes fixes,
hors numéros spéciaux pendant 3 mois.
Pour profiter de cette offre exceptionnelle, cliquez ici : http://register.tiscali.fr/adsl/ (voir conditions sur le site)
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org